CodeForces - 828C String Reconstruction 并查集(next跳)
String Reconstruction
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string tioccurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.
Input
The first line contains single integer n (1 ≤ n ≤ 105) — the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers 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 tidoesn't exceed 106, 1 ≤ xi, j ≤ 106, 1 ≤ ki ≤ 106, and the sum of all ki doesn't exceed 106. The strings 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
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
abacaba
1
a 1 3
aaa
3
ab 1 1
aba 1 3
ab 2 3 5
ababab 寻找一个字符串。给你一些线索(某些子串的位置),输出一个满足条件的最小字典序字符串。
暴力求解超时。可以采用并查集,若当前位置已经有子串放置,那就从已知子串的末尾继续放。并查集记录下末尾的下一位。很好的避免了重叠部分的重复枚举。有点像cys学长教过的那道next跳的题。
并查集套路题。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<math.h>
#include<queue>
#include<set>
#include<stack>
#include<algorithm>
#include<vector>
#include<iterator>
#define MAX 2000005
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std; typedef long long ll; int f[MAX];
char a[MAX];
int find(int x){
return f[x]==x?x:f[x]=find(f[x]);
}
int main()
{
int n,m,x,len,i,j,k,ii;
char s[MAX];
scanf("%d",&n);
for(i=;i<=;i++){
a[i]='a';
f[i]=i;
}
int maxx=;
while(n--){
scanf(" %s %d",s,&k);
len=strlen(s);
for(j=;j<=k;j++){
scanf("%d",&x);
int xx=find(x);
int ff=find(len+x);
for(i=xx,ii=xx-x;i<=len+x-;i++,ii++){
a[i]=s[ii];
f[find(i)]=ff;
}
if(len+x->maxx) maxx=len+x-;
}
}
for(i=;i<=maxx;i++){
printf("%c",a[i]);
}
printf("\n");
return ;
}
最新文章
- Eclipse Maven Spring MyBatis 配置
- gradle多模块开发
- 【C#】Json数据 排版算法
- linux查看内存
- SurfaceView 和 TextureView
- 大数据平台搭建(hadoop+spark)
- pinyin4j
- swift3.0 中NSNotification 的使用
- iOS开发UI篇——简单的浏览器查看程序
- Android minHeight/Width,maxHeight/Width
- 浅析Java中的反射机制原理
- 12. leetcode 455.Assign Cookies
- Java经典编程题50道之四十三
- 17-TypeScript代理模式
- 优雅的使用Linux
- C语言的“编译、链接”
- bash 刷题leetcode
- eclipse:显示堆内存
- windows上dubbo-admin的安装
- Linux如何从零开始搭建rsync服务器(centOS6)