高層タワー [MISSION LEVEL: B]
很久之前就看见过这个网站,今天突然想做一下试试hh
题面
方法
用的kmp。。。搞了好半天
#include<iostream>
#include<cstring>
using namespace std;
const int N = 10010;
char res[2][N];
int cnt, n;
int ne[N];
int main(){
cin >> n;
for(int i = 0; i < n; i ++){
int cur = i & 1, bef = cur ^ 1;
cin >> res[cur];
if(i){
int a = strlen(res[cur]);
int b = strlen(res[bef]);
for(int i = 0, j = ne[0] = -1; i <= a; ne[++ i] = ++ j)
while(~j && res[cur][i] != res[cur][j]) j = ne[j];
int p = 0, q = 0;
while(p < b){
while(~q && res[bef][p] != res[cur][q]) q = ne[q];
++ p, ++ q;
if(q == a && p < b) q = ne[q];
}
for(int j = b; q < a; ++ q, ++ j) res[bef][j] = res[cur][q];
memcpy(res[cur], res[bef], sizeof res[bef]);
}
}
cout << res[(n - 1) & 1 ^ 1];
return 0;
}