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

 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的所有元素

686. Repeated String Match的更多相关文章

  1. 【Leetcode_easy】686. Repeated String Match

    problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...

  2. 【LeetCode】686. Repeated String Match 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. Leetcode 686 Repeated String Match

    Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...

  4. 686. Repeated String Match 字符串重复后的子字符串查找

    [抄题]: Given two strings A and B, find the minimum number of times A has to be repeated such that B i ...

  5. [LeetCode] 686. Repeated String Match 重复字符串匹配

    Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...

  6. 686. Repeated String Match判断字符串重复几次可以包含另外一个

    public static int repeatedStringMatch(String A, String B) { //判断字符串a重复几次可以包含另外一个字符串b,就是不断叠加字符串a直到长度大 ...

  7. 【刷题笔记】686. Repeated String Match

    题意 题目大意是,给两个字符串 A 和 B,问 B 是否能成为 A+A+A+...+A 的子字符串,如果能的话,那么最少需要多少个 A? 暴力解法 直接 A+A+...,到哪次 A 包含 B 了,就返 ...

  8. LeetCode 686. 重复叠加字符串匹配(Repeated String Match)

    686. 重复叠加字符串匹配 686. Repeated String Match 题目描述 给定两个字符串 A 和 B,寻找重复叠加字符串 A 的最小次数,使得字符串 B 成为叠加后的字符串 A 的 ...

  9. LeetCode686——Repeated String Match

    题目:Given two strings A and B, find the minimum number of times A has to be repeated such that B is a ...

随机推荐

  1. 51nod 1459 迷宫游戏 dijkstra模板

    链接:迷宫游戏 问题 - 51Nod  http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1459 1459 迷宫游戏  基准 ...

  2. TOJ 2130: Permutation Recovery(思维+vector的使用)

    传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=2130 时间限制(普通/Java): ...

  3. 二次注入的学习--Buy Flag(http://10.112.68.215:10002)

    这次有做一个二次注入的天枢CTF题目,算是完整地理解了一遍注入的知识.来,启航.   1.判断注入点     经过对题目的实践分析,知道注册时需要输入年龄大于18岁,但在登录后界面,年龄因为太大不能接 ...

  4. xadmin系列之启动、注册、分发

    a.启动首先要加载settings中定义的INSTALLED_APPS列表中的app b.我们进入xadmin的XadminConfig文件 from django.apps import AppCo ...

  5. TableView中Label自适应高度

    //Xcode6.3以后label自适应需要添加两个属性 _tableView.rowHeight = UITableViewAutomaticDimension; //给予预计行高 _tableVi ...

  6. android的Afinal框架下的数据库更新

    项目需要,版本升级时给表添加了一些新的字段,发现出现异常. 解决方法:监听数据库的版本号,更新数据库. 创建FinalDb对象时使用如下的构造函数,监听版本号的变化: db = FinalDb.cre ...

  7. cgi fast-cgi php-fpm区别

    php-cli 是php在系统执行的程序,直接执行php文件: cgi和fast-cgi的区别1.cgi和fast-cgi都是php解析协议,负责解析服务器分发过来的php动态文件:cgi程序就会去解 ...

  8. 解决jenkins的内存溢出问题

    在jenkins的控制台会看到如下信息: FATAL: Remote call on ime_checkcode failed java.io.IOException: Remote call on ...

  9. mvc中webapi添加后没法访问 解决办法

    原因:原先项目中没有webapi,后来添加的. 然后就没法正常访问,百度了下发现是 App_Start/WebApiConfig.cs中路由配置多了个api 而且没有加{action}, 然后修改成: ...

  10. sqlite c#

    https://www.cnblogs.com/icebutterfly/p/7850689.html https://www.cnblogs.com/sdadx/p/7127098.html