题目链接:https://leetcode-cn.com/problems/repeated-substring-pattern/
题目如下:
class Solution {
public:
void getNext(int* next,const string& s){
int j=0;
next[0]=0;
for(int i=1;i<s.size();i++){
while(j>0&&s[i]!=s[j]){
j=next[j-1];
}
if(s[i]==s[j]) j++;
next[i]=j;
}
}
bool repeatedSubstringPattern(string s) {
if(s.size()==0) return false;
int next[s.size()];
getNext(next,s);
int len=s.size();
//最长相等前后缀的长度为next[len-1]+1 数组长度为len
//如果len%{len-(next[len-1]+1)}==0,则说明可以被数组的长度整除
if(next[len-1]!=0&&len%(len-(next[len-1]))==0) return true;
else return false;
}
};