(80)459. Repeated substring (leetcode)*

题目链接:
https://leetcode-cn.com/problems/repeated-substring-pattern/
难度:简单
459. 重复的子字符串
	给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。
示例 1:
	输入: "abab"
	输出: True
	解释: 可由子字符串 "ab" 重复两次构成。
示例 2:
	输入: "aba"
	输出: False
示例 3:
	输入: "abcabcabcabc"
	输出: True
	解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。)

To be a mark this still needs to be seen. . . This is the fifth time I have said such things and haven't seen the grass a few times before! So I'm so spicy. . .
Simply doing this question is not difficult, it can be said to be very simple. . . . Just enumerate directly, and then string matching must have kmp (intuition). . . . .

class Solution {
    
    
public:
    bool repeatedSubstringPattern(string s) {
    
    
        int n=s.size();
        int i=1;
        while(i<=(n/2)){
    
    
            if(n%i!=0){
    
    
                i++;
                continue;
            }
            bool flag=true;
            for(int j=i;j<n;++j){
    
    
                if(s[j]!=s[j-i]){
    
    
                    flag=false;
                    break;
                }
            }
            if(flag){
    
    
                return true;
            }
            ++i;
        }
        return false;
    }
};

Then the important point comes.
The requirement of this question can be transformed into: connect two s together and remove the first and last characters. If s is a substring of the string, then s meets the requirements of the question. Well, it proves not. . .
After knowing this, then use the c++ library

class Solution {
    
    
public:
    bool repeatedSubstringPattern(string s) {
    
    
        return (s + s).find(s, 1) != s.size();
    }
};

emmm. . . I didn't think that the solution would be like this

Then there is the key kmp. . . md This is the fourth time I have written kmp algorithm. . I forgot and didn't write it out, or I didn't fully understand it. I have time to write about the kmp algorithm and set up a flag (the seventh one has six questions that I can't understand and questions that I don't fully understand). . . . . . . . Still have fun

class Solution {
    
    
public:
    bool kmp(const string& query, const string& pattern){
    
    
        int n = query.size();
        int m = pattern.size();
        vector<int> nextTable(m, -1);
        // 求出nextTable数组
        for (int i = 1; i < m; ++i) {
    
    
            int j = nextTable[i - 1];
            while (j != -1 && pattern[j + 1] != pattern[i]) {
    
    
                j = nextTable[j];
            }
            if (pattern[j + 1] == pattern[i]) {
    
    
                nextTable[i] = j + 1;
            }
        }
        // 开始 注意这里 从 1开始 到 n-1结束 
        //(去除第一和最后一个字符)
        int match=-1;
        for(int i=1;i<n-1;++i){
    
    
            while (match != -1 && pattern[match + 1] != query[i]) {
    
    
                match = nextTable[match];
            }
            if (pattern[match + 1] == query[i]) {
    
    
                ++match;
                if (match == m - 1) {
    
    
                    return true;
                }
            }
        }
        return false;
    }
    bool repeatedSubstringPattern(string s) {
    
    
        return kmp(s+s,s);
    }
};

The above algorithm can only be understood by looking at the description of the algorithm,
but the following one does not understand what it means! . . .
Record a coordinate to check leetcode official problem solution in the future

class Solution {
    
    
public:
    bool kmp(const string& pattern){
    
    
        int n=pattern.size();
        vector<int> nextTable(n, -1);
        for (int i = 1; i < n; ++i) {
    
    
            int j = nextTable[i - 1];
            while (j != -1 && pattern[j + 1] != pattern[i]) {
    
    
                j = nextTable[j];
            }
            if (pattern[j + 1] == pattern[i]) {
    
    
                nextTable[i] = j + 1;
            }
        }
        return nextTable[n - 1] != -1 && n % (n - nextTable[n - 1] - 1) == 0;;
    }
    bool repeatedSubstringPattern(string s) {
    
    
        return kmp(s);
    }
};

Guess you like

Origin blog.csdn.net/li_qw_er/article/details/108210183