Problem Description
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s s s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s s s. Namely, he remembers, that string t i ti ti occurs in string s s s at least k i ki ki times or more, he also remembers exactly k i ki ki positions where the string ti occurs in string s s s: these positions are x i , 1 , x i , 2 , . . . , x i , k i xi, 1, xi, 2, ..., xi, ki xi, 1, xi, 2, ..., xi, ki. He remembers n n n such strings t i ti ti.
You are to reconstruct lexicographically minimal string s s s such that it fits all the information Ivan remembers. Strings t i ti ti and string s s s consist of small English letters only.
Input
The first line contains single integer n ( 1 ≤ n ≤ 1 0 5 ) n (1 ≤ n ≤ 10^{5}) n(1 ≤ n ≤ 105) — the number of strings Ivan remembers.
The next n n n lines contain information about the strings. The i i i-th of these lines contains non-empty string t i ti ti, then positive integer k i ki ki, which equal to the number of times the string t i ti ti occurs in string s s s, and then k i ki ki distinct positive integers x i , 1 , x i , 2 , . . . , x i , k i xi, 1, xi, 2, ..., xi, ki xi, 1, xi, 2, ..., xi, ki in increasing order — positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn’t exceed 1 0 6 10^{6} 106, 1 ≤ x i , j ≤ 1 0 6 , 1 ≤ k i ≤ 1 0 6 1 ≤ xi, j ≤ 10^{6}, 1 ≤ ki ≤ 10^{6} 1 ≤ xi, j ≤ 106,1 ≤ ki ≤ 106, and the sum of all ki doesn’t exceed 1 0 6 10^{6} 106. The strings t i ti ti can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
Output
Print lexicographically minimal string that fits all the information Ivan remembers.
Examples
Input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
Output
abacaba
Input
1
a 1 3
Output
aaa
Input
3
ab 1 1
aba 1 3
ab 2 3 5
Output
ababab
题目大意:
现在让你构造出来一个最小字典序的字符串,使得其满足n个条件。这n个条件中每个条件有一个连续的子字符串。然后有Ki个位子,表示这些字符串出现的位子。保证有解,n个条件互相没有矛盾。
思路:
直接暴力每次在Ki个点处作为起点,然后向后涂这个子字符串,因为给出的位置是递增的,如果已经涂过的位子不用再涂,最后把没赋值的部分换成‘a’。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
char a[2010000],s[2010000];
int main(){
int n;
while(~scanf("%d",&n)){
int maxx=-1;
memset(s,-1,sizeof(s));
for(int i=0;i<n;i++){
scanf("%s",&a);
int n=strlen(a);
int m;
scanf("%d",&m);
int t=-inf;
for(int i=0;i<m;i++){
int z;
scanf("%d",&z);
maxx=max(maxx,z+n-1);
int k=0;
for(int j=max(z-1,t);j<z+n-1;j++)
s[j]=a[j-z+1];
t=z+n-1;
}
}
for(int i=0;i<maxx;i++)
if(s[i]==-1)
printf("a");
else
printf("%c",s[i]);
printf("\n");
}
return 0;
}
下面使用的是并查集
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2010000;
char a[maxn],s[maxn];
int f[maxn];
int getf(int x){
return f[x]==x?x:f[x]=getf(f[x]);
}
void init(){
for(int i=0;i<=maxn;i++){
a[i]='a';
f[i]=i;
}
}
int main(){
int n,m,x,len,k;
init();
scanf("%d",&n);
int maxx=0;
while(n--){
scanf("%s%d",s,&k);
len=strlen(s);
for(int j=1;j<=k;j++){
scanf("%d",&x);
int xx=getf(x);
int ff=getf(len+x);
for(int i=xx,ii=xx-x;i<=len+x-1;i++,ii++){
a[i]=s[ii];
f[getf(i)]=ff;
}
maxx=max(maxx,len+x-1);
}
}
for(int i=1;i<=maxx;i++){
printf("%c",a[i]);
}
printf("\n");
return 0;
}
实践是检验真理的唯一标准