题意:给出若干子串(length<=1e6),每个子串给出若干index(小于1e6个),表示这个子串在原串的这些地方出现了,求字典序最小的原串,保证一定存在。
题解:保证一定存在的意思是,用char去填空,不会出现冲突的情况。只需要把每个串的每个index都填一遍,然后空位换成‘a’输出就可以了。现在的问题是,串太多,index太多,要降低复杂度。因为不存在冲突的情况,因此填上字符的位置,不需要再做检查和重复填写,因此用并查集维护连续的finished的线段。(这不就是0$大腿给我讲的那个并查集的题吗。。。嗯。确实想到了。但是并查集写起来还是要注意一下,要不然路径压缩失效还是会TLE。注意合并的时候是father[i]=find(i+1);而不是father[i]=father[i+1]这样没有路径压缩,会gg掉。
Code:
#include<bits/stdc++.h>
using namespace std;
#define MAX 1000006
int father[MAX*3];
char ans[MAX*3];
char temp [MAX*3];
int n;
int maxlength = -1;
int find(int x){
return father[x]==x?x:father[x]=find(father[x]);
}
int main(){
for (int i=0;i<MAX*3;i++){
father[i]=i;
}
cin>>n;
for (int o=0;o<n;o++){
int num;
scanf("%s %d",temp,&num);
int length = strlen(temp);
for (int i=0;i<num;i++){
int start ;
scanf("%d",&start);
int now = father[start];
while (now<start+length){
ans[now]=temp[now-start];
father[now]=find(now+1);
now=father[now];
}
maxlength = max (maxlength,start+length);
}
}
for (int i=1;i<maxlength;i++){
if (ans[i]>='a'&&ans[i]<='z'){
cout<<ans[i];
}else{
cout<<'a';
}
}
return 0;
}