Codeforces - 828C String Reconstruction —— 并查集find()函数

时间:2021-12-14 09:40:31

题目链接:http://codeforces.com/contest/828/problem/C

C. String Reconstruction
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 ti occurs 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 ti doesn'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
input
Copy
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
output
Copy
abacaba
input
Copy
1
a 1 3
output
Copy
aaa
input
Copy
3
ab 1 1
aba 1 3
ab 2 3 5
output
Copy
ababab

题意:

求一个字典序最小的字符串,满足若干个字符串在此字符串中的出现情况。

采取逐个位置写入的方法。为了避免超时,写过的位置就不能重新写,而需找到后面第一个没有被写入的位置。那如果找到这个位置呢?如果直接往后遍历,那就跟重复写入没有区别了,所以,我们需要一个快速找到后面第一个没有被写入的位置的方法,可用并查集的find()函数。灵感来源:POJ1456 Supermarket 。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e6+; int fa[MAXN]; //fa[]用于记录此位置后面第一个没有被写入位置
bool used[MAXN];
int find(int x)
{
return !used[x]?x:fa[x]=find(fa[x]);
} char s[MAXN], tmp[MAXN];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(s, , sizeof(s));
memset(used, , sizeof(used));
for(int i = ; i<MAXN-; i++)
fa[i] = i+;
for(int i = ; i<=n; i++)
{
scanf("%s",tmp);
int sz = strlen(tmp);
int m, x;
scanf("%d",&m);
while(m--)
{
scanf("%d",&x);
for(int i = ; i<sz;)
{
if(!s[x+i]) //此位置没有被写入,则填写
{
s[x+i] = tmp[i];
used[x+i] = ;
i++;
}
else i = find(x+i)-x; //否则,找到后面位置第一个没有被写入的相对位置
}
}
} int sz = MAXN-;
while(!s[sz]) sz--;
for(int i = ; i<=sz; i++)
{
if(s[i]==) putchar('a'); //把没有写入的位置都填上‘a’
else putchar(s[i]);
}
putchar('\n');
}
}