Code & Fun

第91天。

今天的题目是Repeated Substring Pattern:

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.
Example 1:
Input: “abab”

Output: True

Explanation: It’s the substring “ab” twice.
Example 2:
Input: “aba”

Output: False
Example 3:
Input: “abcabcabcabc”

Output: True

Explanation: It’s the substring “abc” four times. (And the substring “abcabc” twice.)

一开始没看清,以为只有重复一次的情况,后来发现还可以重复多次,这样的话就不得不多扫描几遍了,有点希尔排序的解法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool repeatedSubstringPattern1(string s) {
for(int i = 1;i < s.size();i++)
if (repeatedSubstringPattern(s,i)) return true;
return false;
}
bool repeatedSubstringPattern(string &s,int p) {
// cout << p << endl;
int size = s.size();
if (size % p) return false;
for(int i = 0;i < p;i++) {
for(int j = i+p;j < size;j += p) {
//cout << s[i] << " " << s[j] << endl;
if (s[i] != s[j]) return false;
}
}
return true;
}

然后是在dicuss中的利用kmp的解法,但我还是没看懂为什么可以这样做。

1
2
3
4
5
6
7
8
9
10
bool repeatedSubstringPattern(string str) {
int i = 1, j = 0, n = str.size();
vector<int> dp(n+1,0);
while( i < str.size() ){
if( str[i] == str[j] ) dp[++i]=++j;
else if( j == 0 ) i++;
else j = dp[j];
}
return dp[n]&&dp[n]%(n-dp[n])==0;
}

本文首发于Code & Fun