LeetCode Weekly Contest 52 Repeated String Match(string)

题目:https://leetcode.com/contest/leetcode-weekly-contest-52/problems/repeated-string-match/
题意:让你求最少重复A多少次,才能在使得B是A的子串;如果不能,输出-1
思路:我竟然懵逼的写了kmp,还wa了几次……
直接string就行
代码:

class Solution {
public:
    int repeatedStringMatch(string A, string B) {
        int cnt = 1;
        string str = A;
        while(str.length() < B.length())
            str += A,cnt++;
        if(str.find(B) != -1)
            return cnt;
        str += A,cnt++;
        if(str.find(B) != -1)
            return cnt;
        return -1;
    }
};

你可能感兴趣的:(易题)