【CodeForces - 827A】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?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
3 a 4 1 3 5 7 ab 2 1 5 ca 1 4Output
abacabaInput
1 a 1 3Output
aaaInput
3 ab 1 1 aba 1 3 ab 2 3 5Output
ababab題目大意:
有一個字符串,最長1e6,現在給你若干個它的子字符串,每個子字符串長度不超過1e6,最多給你1e5個子字符串,告訴你第i個子字符串出現了ki次,并告訴你它每次出現的第一個字符的位置。所有子字符串的長度之和不超過1e6,出現的次數總和也不超過1e6。然后問你最短的滿足要求的字符串中字典序最小的那個是什么。
輸入是這樣的:
第一行一個數n,代表有n個子字符串。
接下來n行,第i行 先是一個子字符串,然后一個數ki,然后ki個數,每個數代表第i個子字符串的第一個數分別出現的位置。
輸入合法。
解題報告:
? 因為構造出的字符串最多1e6且輸入合法,所以直接在每個位置填充就可以了,當然暴力填充的話可能會有很多重復填充,所以用并查集維護一下第i個字符后面第一個空位。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 5e6 + 5; int mx,f[MAX]; char s[MAX],ans[MAX]; int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]); } int main() {int n;cin>>n;for(int i = 1; i<MAX; i++) f[i] = i,ans[i] = 'a';for(int num,x,len,i = 1; i<=n; i++) {scanf("%s%d",s+1,&num);len=strlen(s+1);for(int j = 1; j<=num; j++) {scanf("%d",&x);mx = max(mx,x+len-1);for(int k = x; k<=x+len-1; k=getf(k+1)) {ans[k] = s[k-x+1];f[k] = k+1;//=f[k+1]或者getf(k+1)其實都可以,只要記得在使用的時候getf一下就好,這題是巧了k就是根節點了,所以可以這樣操作。也就是并查集使用的時候要保證,操作的節點必須是根節點,但是不一定接在人家的根節點上。}}} for(int i = 1; i<=mx; i++) printf("%c",ans[i]);return 0 ; }?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的【CodeForces - 827A】String Reconstruction(并查集合并区间,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 女子被80斤重狗狗挤下沙发大哭摔到尾椎骨
- 下一篇: 【Python学习】 - 使用PIL,