给出一个非空的字符串,判断这个字符串是否是由它的一个子串进行多次首尾拼接构成的。
例如,"abcabcabc"满足条件,因为它是由"abc"首尾拼接而成的,而"abcab"则不满足条件。
输入描述:
非空字符串
输出描述:
如果字符串满足上述条件,则输出最长的满足条件的的子串;如果不满足条件,则输出false。
#include<iostream>
#include<string>
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
int length = s.size();
int len = (length+1) / 2;
int i, j;
for (i = len-1; i >= 0; i--){
for (j = i+1; j < length; j++){
if (s[j] == s[j % (i+1)]){
continue;
}
else{
break;
}
}
if (j == length&&(j-1)%(i+1)==i){
break;
}
}
if (i < 0){
cout << false;
}
for (int t = 0; t < i + 1; t++){
cout << s[t];
}
system("pause");
}