Codeforces 828C String Reconstruction【並查集巧妙運用】
阿新 • • 發佈:2018-11-10
題目大意
給你n個串和在原串中的出現位置,問原串
思路
直接跑肯定是GG
考慮怎麼優化
因為保證有解,所以考慮過的點我們就不再考慮
用並查集維護當前每個點之後最早的沒有被更新過的點
然後就做完了,很巧妙對吧
c++//Author: dream_maker #include<bits/stdc++.h> using namespace std; //---------------------------------------------- //typename typedef long long ll; //convenient for #define fu(a, b, c) for (int a = b; a <= c; ++a) #define fd(a, b, c) for (int a = b; a >= c; --a) #define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a) //inf of different typename const int INF_of_int = 1e9; const ll INF_of_ll = 1e18; //fast read and write template <typename T> void Read(T &x) { bool w = 1;x = 0; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') w = 0, c = getchar(); while (isdigit(c)) { x = (x<<1) + (x<<3) + c -'0'; c = getchar(); } if (!w) x = -x; } template <typename T> void Write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) Write(x / 10); putchar(x % 10 + '0'); } //---------------------------------------------- const int N = 1e6 + 10; int n, fa[N << 1]; char s[N << 1], c[N]; void init() { fu(i, 1, (N << 1) - 1) fa[i] = i; } int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } int main() { Read(n); init(); int maxl = 0; fu(i, 1, n) { scanf("%s", c + 1); int num, len = strlen(c + 1), pos; Read(num); fu(j, 1, num) { Read(pos); maxl = max(maxl, pos + len - 1); for (int k = find(pos); k <= pos + len - 1; k = find(k)){ s[k] = c[k - pos + 1]; fa[k] = k + 1; } } } fu(i, 1, maxl) if (!s[i]) s[i] = 'a'; printf("%s", s + 1); return 0; }