String Reconstruction CodeForces - 827A Greedy Pseudo-String Matching

Topic link:
String Reconstruction Main
idea:
It is easy to understand by looking at the example, giving the characters and their positions, constructing a specific string, and filling the remaining positions with 'a'

input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
output
abacaba
input
1
a 1 3
output
aaa

Idea:
The result given by the title is specific, and there is no need to judge whether it can be constructed.
Remove the repeated places. If you do not repeat assignments, you will not have TLE
. The way to avoid repeated assignments is to sort them according to the coordinate positions.
Set a cursor to scan once, no Position +='a'

Code:

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define inf 0x3f3f3f3f
#define mem(s,t) memset(s,t,sizeof s)
#define mk make_pair
typedef pair<int, int> pii;
typedef long long ll;
const int mod = 1e9 + 7;
const int MAXN = 1e6 + 10;
#define D(v) cout<<#v<<" "<<v<<endl
#define fi first
#define se second
int cnt=0;
pii a[MAXN];
string s[MAXN];
int main() {
    ios::sync_with_stdio(false);
    int T,n;
    cin>>T;
    for(int i=1;i<=T;i++){
        cin>>s[i]>>n;
        for(int j=1;j<=n;j++){
            int x;
            cin>>x;
            a[cnt++]=mk(x,i);
        }
    }
    sort(a,a+cnt);
    int cur=1;
    string ans="";
    for(int i=0;i<cnt;i++){
        while(cur<a[i].fi) cur++,ans+='a';
        for(int j=cur-a[i].fi;j<s[a[i].se].size();j++){//for循环才是优化的地方
            ans+=s[a[i].se][j];
            cur++;
        }
    }
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325407084&siteId=291194637