String Reconstruction CodeForces - 827A 贪心 伪字符串匹配

题目链接:
String Reconstruction
大意:
看样例很容易懂,给出出现的字符及其位置,构造出给特定的字符串,其余位置用 ‘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

思路:
题目给出结果是特定的,不需要判断是否能构造出
将重复的地方除去,不要重复赋值就不会TLE
避免重复赋值的方法就是按照坐标位置排好序
设置一个光标扫描一遍,没有的位置+=’a’即可

代码实现:

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值