Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 828C) - 链表 - 并查集
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 stringsti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string sconsist of small English letters only.
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 exceed106, 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.
Print lexicographically minimal string that fits all the information Ivan remembers.
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
我同学都用排序 + 并查集,然而我用链表 + 并查集思想。
因为数据合法不用判错,所以用链表维护还未确定的位置。每条信息,找第一个确定的位置时边找边压缩路径(并查集思想)。然后按题意弄一下即可。
另外吐槽一下这道题。。直接交代码在第十个点各种WA,RE(必须交G++14才能AC,而且跑得非常慢),然后cf上交文件AC。。(可能是你在逗我)
Code
1 /** 2 * Codeforces 3 * Problem#828C 4 * Accepted 5 * Time:296ms 6 * Memory:59888k 7 */ 8 #include <iostream> 9 #include <cstdio> 10 #include <ctime> 11 #include <cmath> 12 #include <cctype> 13 #include <cstring> 14 #include <cstdlib> 15 #include <fstream> 16 #include <sstream> 17 #include <algorithm> 18 #include <map> 19 #include <set> 20 #include <stack> 21 #include <queue> 22 #include <vector> 23 #include <stack> 24 #ifndef WIN32 25 #define Auto "%lld" 26 #else 27 #define Auto "%I64d" 28 #endif 29 using namespace std; 30 typedef bool boolean; 31 const signed int inf = (signed)((1u << 31) - 1); 32 const signed long long llf = (signed long long)((1ull << 61) - 1); 33 const double eps = 1e-6; 34 const int binary_limit = 128; 35 #define smin(a, b) a = min(a, b) 36 #define smax(a, b) a = max(a, b) 37 #define max3(a, b, c) max(a, max(b, c)) 38 #define min3(a, b, c) min(a, min(b, c)) 39 template<typename T> 40 inline boolean readInteger(T& u){ 41 char x; 42 int aFlag = 1; 43 while(!isdigit((x = getchar())) && x != '-' && x != -1); 44 if(x == -1) { 45 ungetc(x, stdin); 46 return false; 47 } 48 if(x == '-'){ 49 x = getchar(); 50 aFlag = -1; 51 } 52 for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0'); 53 ungetc(x, stdin); 54 u *= aFlag; 55 return true; 56 } 57 58 int n; 59 int m = 0; 60 string* strs; 61 vector<int> *pos; 62 boolean* exist; 63 char* res; 64 int* suf; 65 int* pre; 66 67 inline void init() { 68 readInteger(n); 69 strs = new string[n]; 70 pos = new vector<int>[n]; 71 for(int i = 0; i < n; i++) { 72 static int c, x, l; 73 cin >> strs[i]; 74 l = strs[i].length(); 75 readInteger(c); 76 for(int j = 0; j < c; j++) { 77 readInteger(x); 78 pos[i].push_back(x); 79 smax(m, x + l - 1); 80 } 81 } 82 } 83 84 inline void remove(int x) { 85 pre[suf[x]] = pre[x]; 86 suf[pre[x]] = suf[x]; 87 exist[x] = false; 88 } 89 90 int find(int x, int endpos) { 91 if(exist[x] || x >= endpos) return x; 92 return suf[x] = find(suf[x], endpos); 93 } 94 95 inline void solve() { 96 exist = new boolean[m + 2]; 97 suf = new int[m + 2]; 98 pre = new int[m + 2]; 99 res = new char[m + 2]; 100 memset(exist, true, sizeof(boolean) * (m + 2)); 101 suf[n + 1] = m + 1, pre[0] = 0; 102 for(int i = 0; i <= m; i++) suf[i] = i + 1; 103 for(int i = 1; i <= m + 1; i++) pre[i] = i - 1; 104 105 for(int i = 0; i < n; i++) { 106 int l = strs[i].length(); 107 for(int j = 0; j < (signed)pos[i].size(); j++) { 108 int p = pos[i][j], start = pos[i][j]; 109 p = find(p, start + l); 110 while(p < pos[i][j] + l) { 111 res[p] = strs[i][p - start]; 112 remove(p); 113 p = suf[p]; 114 } 115 } 116 } 117 118 for(int i = 1; i <= m; i++) 119 if(exist[i]) 120 res[i] = 'a'; 121 res[m + 1] = 0; 122 123 puts(res + 1); 124 } 125 126 int main() { 127 init(); 128 solve(); 129 return 0; 130 }