CodeForces-1029A Many Equal Substrings
题目链接:CodeForces-1029A
题目大意:给你一个子串 让你输出一个母串,使得母串中包含k个子串
解题思路:利用kmp算法求next数组的部分,求得需要往后拼接的子串的字符数,之后往后拼接即可
代码块:
#include<iostream>
#include<cstdio>
using namespace std;
char strStr[60] = {'\0'};
char strSub[2600] = {'\0'};
int _next[60] = {0};
void getNext(int x){
int i = 1, j = 0;
while(i < x){
if(strStr[i] == strStr[j]){
_next[i++] = ++j;
}else if(j != 0){
j = _next[j - 1];
}else{
_next[i++] = 0;
}
}
}
int main(){
int m, n;
cin >> m >> n;
scanf("%s", strStr);
getNext(m);
int temp = _next[m - 1];
int i = 0;
for(i = 0; i < m; i++){
strSub[i] = strStr[i];
}
int flag = 1;
while(flag < n){
for(int j = temp; j < m; j++){
strSub[i++] = strStr[j];
}
flag++;
}
cout<<strSub;
return 0;
}