Many Equal Substrings CF - 1029A (KMP)
题意:给你n个由小写字母组成的字符串S,求出能找到k个S的新字符串,并且尽量短
考察nxt数组的含义
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int maxn = 4e5 + 10; int n,k; char s[maxn]; int l; int nxt[maxn]; int ans[maxn]; void work() { for (int i = 2, j = 0; i <= l; i++) { while (j > 0 && s[i] != s[j + 1]) j = nxt[j]; if (s[i] == s[j + 1]) j++; nxt[i] = j; } } int main() { //freopen("in","r",stdin); scanf("%d %d",&n,&k); scanf("%s", s + 1); l = strlen(s + 1); work(); printf("%s",s+1); k--; while(k--){ for(int i = nxt[l] + 1;i <= l; i++) printf("%c",s[i]); } printf("\n"); return 0; }