Codeforces 828 C String Reconstruction

本文提供了一道CodeForces C题目的解题思路及代码实现。题目要求根据给定的字符串提示,拼接出字典序最小的完整字符串。通过排序起点并遍历的方法,避免重复计算,实现了高效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址:http://codeforces.com/contest/828/problem/C
题意:给你一些字符串的提示,让你拼接出一个完整的字典序最小的字符串(PS:是一定会有字符串的,所以重复的可以不用去看,因为一定是对的,我就是一开始没看懂这个TLE了)
思路:把起点的顺序排序,再遍历。重复的就不用考虑,详细看代码,比较容易理解,仔细点就好了

#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdio>
#include <algorithm>
#define LL long long 
#define N 10000010
#define M 50010
#define inf 0x3f3f3f3f
using namespace std;
const LL mod = 1e9 + 7;
const double eps = 1e-9;
string str[N];
struct node {
    int id, x;
}now;
vector<node>v;
bool cmp(node a, node b) {
    if (a.x == b.x) {
        return str[a.id].length() > str[b.id].length();
    }
    return a.x < b.x;
}
int main() {
    cin.sync_with_stdio(false);
    int n, m;
    while (cin >> n) {
        v.clear();
        for (int i = 0; i < n; i++) {
            cin >> str[i] >> m;
            now.id = i;
            for (int j = 0; j < m; j++) {
                cin >> now.x;
                v.push_back(now);
            }
        }
        sort(v.begin(), v.end(), cmp);
        int len = v.size();
        int num = 1;
        for (int i = 0; i < len; i++) {
            while (num < v[i].x) {
                num++;
                cout << "a";
            }
            if (num != v[i].x) {
                if (num > v[i].x + str[v[i].id].length()) {
                    continue;
                }
                for (int j = num - v[i].x; j < str[v[i].id].length(); j++) {
                    cout << str[v[i].id][j];
                    num++;
                }
            }
            else {
                cout << str[v[i].id];
                num += str[v[i].id].length();
            }
        }
        cout << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值