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

题目链接: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 。

 

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <cstdlib>
 6 #include <string>
 7 #include <vector>
 8 #include <map>
 9 #include <set>
10 #include <queue>
11 #include <sstream>
12 #include <algorithm>
13 using namespace std;
14 typedef long long LL;
15 const double eps = 1e-8;
16 const int INF = 2e9;
17 const LL LNF = 9e18;
18 const int MOD = 1e9+7;
19 const int MAXN = 2e6+10;
20 
21 int fa[MAXN];   //fa[]用于记录此位置后面第一个没有被写入位置
22 bool used[MAXN];
23 int find(int x)
24 {
25     return !used[x]?x:fa[x]=find(fa[x]);
26 }
27 
28 char s[MAXN], tmp[MAXN];
29 int main()
30 {
31     int n;
32     while(scanf("%d",&n)!=EOF)
33     {
34         memset(s, 0, sizeof(s));
35         memset(used, 0, sizeof(used));
36         for(int i = 1; i<MAXN-1; i++)
37             fa[i] = i+1;
38         for(int i = 1; i<=n; i++)
39         {
40             scanf("%s",tmp);
41             int sz = strlen(tmp);
42             int m, x;
43             scanf("%d",&m);
44             while(m--)
45             {
46                 scanf("%d",&x);
47                 for(int i = 0; i<sz;)
48                 {
49                     if(!s[x+i]) //此位置没有被写入,则填写
50                     {
51                         s[x+i] = tmp[i];
52                         used[x+i] = 1;
53                         i++;
54                     }
55                     else i = find(x+i)-x;   //否则,找到后面位置第一个没有被写入的相对位置
56                 }
57             }
58         }
59 
60         int sz = MAXN-1;
61         while(!s[sz]) sz--;
62         for(int i = 1; i<=sz; i++)
63         {
64             if(s[i]==0) putchar('a');   //把没有写入的位置都填上‘a’
65             else putchar(s[i]);
66         }
67         putchar('\n');
68     }
69 }
View Code

 

 

posted on 2018-04-22 21:20  h_z_cong  阅读(163)  评论(0编辑  收藏  举报

导航