首页 技术 正文
技术 2022年11月10日
0 收藏 360 点赞 2,840 浏览 726 个字

方法一、算是暴力解法吧,拼一段找一下

 static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int repeatedStringMatch(string A, string B)
{
string as=A;
int count=B.size()/A.size()+;
for(int i=;i<count;i++,as+=A)
{
if(as.find(B)!=string::npos)
return i;
}
return -;
}
};

方法二、稍微有点技术含量了,运行速度快些

 static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int repeatedStringMatch(string A, string B)
{
string tmp=A+A;
size_t pos=tmp.find(B.substr(,A.size()));
if(pos==string::npos)
return -;
int count=;
size_t i=;
while(i<B.size())
{
if(pos==A.size())
{
pos=;
++count;
}
if(A[pos++]!=B[i++])
return -;
}
return count;
}
};

先把两个A拼起来成为tmp,在tmp中找B的前面一截,若没找到,则B必然不能成为A拼接序列的子串

若找到了,再进行下面的判定,一个字符一个字符来,扫到A末尾则回到A首部并增加计数器,直到找到B的所有元素

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,116
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,588
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,434
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,204
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,840
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,925