题目:
Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.
For example, with A = "abcd" and B = "cdabcdab".
Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of it; and B is not a substring of A repeated two times ("abcdabcd").
Note:
The length of A
and B
will be between 1 and 10000.
http://blog.youkuaiyun.com/zsheng_/article/details/78153614
class Solution {
public:
int repeatedStringMatch(string A, string B) {
string ss=A;
int ans=1;
if(ss.length()>=B.length())
if(ss.find(B)==0)
return ans;
while(ss.length()<B.length())
{
ss+=A;
ans++;
}
if(ss.find(B)!=-1)
return ans;
ss+=A;
ans++;
if(ss.find(B)!=-1)
return ans;
return -1;
}
};
!!!这里补充find函数知识点:
1.find():查找第一次出现的目标字符串,如果查找成功则输出查找到的第一个位置,否则返回-1;
查找从指定位置开始的第一次出现的目标字符串: