题目描述:

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.

要完成的函数:

int repeatedStringMatch(string A, string B)

说明:

1、给定两个字符串A和B,要求判断需要重复A几次,才能在A中找到B,也就是使B成为A的子串。返回重复的最小次数。

2、这道题题意清晰,解决题目的关键在于把可能的情况想清楚。

本题分为三种情况处理:

① A比B短,这是最容易想到的情况,比如A为“abc”,B为“bcab”,我们要重复1次,使得A的长度大于等于B。接着猜判断B能不能在A中找到,这时我们用find函数即可。

  还有另一种情况,A重复完之后刚好长度等于B,但是我们有时能找到B,有时还要再重复一次才能找到B。

比如A为“abc”,B为“abcabc”,这种就是重复1次,长度刚好等于B,能找到B的情况。

比如A为“abc”,B为“bcabca”,这种就是重复1次,长度刚好等于B,但不能找到B的情况,要再重复1次。

② A比B长,有两种情况,第一种就是刚好能找到,不用重复。比如A为“abcdefg”,B为“bcd”。

  另一种是找不到,要再重复一次,比如A为“abcdefg”,B为“efga”,要再重复一次才能找到。

③ A和B一样长,同样两种情况,第一种就是刚好能找到,A==B

另一种就是要再重复一次,比如A为“abcdefg”,B为“efgabcd”,要再重复一次才能找到。

综上所述,我们使得A的长度大于等于B,如果这个时候能找到B,那么ok,返回重复的次数。

如果不能找到B,那么再重复一次A,如果重复之后能找到,那么返回新的重复的次数。

如果还是找不到,我们认为当A的长度大于等于B的时候,这时候可能还会找不到B,但如果再重复一次A,重复之后还是找不到B,那么就是不可能通过重复A来找到B的,返回-1。(这一点不认同的同学们可以自己再想一想,有问题欢迎在下方评论区交流)

代码如下:(附详解)

    int repeatedStringMatch(string A, string B)
{
string newA;
int count=0;
while(newA.size()<B.size())//重复A使得newA的长度大于等于B
{
newA+=A;
count++;//记录重复的次数
}
if(newA.find(B)!=-1)//如果找得到,返回重复次数
return count;
newA+=A;//如果找不到,再重复一次A
if(newA.find(B)!=-1)//如果找得到,返回新的重复次数
return ++count;
return -1;//如果还是找不到,返回-1
}

上述代码实测17ms,beats 82.81% of cpp submissions。

leetcode-686-Repeated String Match(重复多少次A能够找到B)的更多相关文章

  1. [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 ...

  2. 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 ...

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

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

  4. 【Leetcode_easy】686. Repeated String Match

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

  5. [LeetCode] 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 字符串重复后的子字符串查找

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

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

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

  8. Leetcode686.Repeated String Match重复叠加字符串匹配

    给定两个字符串 A 和 B, 寻找重复叠加字符串A的最小次数,使得字符串B成为叠加后的字符串A的子串,如果不存在则返回 -1. 举个例子,A = "abcd",B = " ...

  9. 686. Repeated String Match

    方法一.算是暴力解法吧,拼一段找一下 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); cl ...

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

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

随机推荐

  1. 使用twised实现一个EchoServer

    ProtocolsProtocols描述了如何以异步的方式处理网络中断时间,HTTP.DNS已经IMAP是应用应用层协议中的例子,Protocols实现了IProtocol接口,它饱和如下的方法 ma ...

  2. php防止会话固定攻击

    问题:希望确保应用不会受到会话固定攻击,即攻击者强制用户使用一个预定义的会话id. 解决方案:要求使用会话cookie但会话标识符不追加到URL,另外要频繁地生成新会话ID: <?php ini ...

  3. mybatis整合spring的完整过程

    1.1 整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得sqlsession对象. 3.Mappe ...

  4. C语言压缩/解压缩

    一.简介 Lzlib 压缩库提供了在内存中的 LZMA 压缩和解压算法功能,包括对数据进行完整性检查.压缩格式是 lzip 参考: http://blog.csdn.net/damenhanter/a ...

  5. eigen安装

    https://blog.csdn.net/liuxiaoheng1992/article/details/54410148

  6. [Selenium]等待元素出现之后再消失,界面上的loading icon都属于这种类型,之前的方法总是卡死,换这种方法目前还好用的

    等待元素出现之后再消失,界面上的loading icon都属于这种类型,之前的方法总是卡死,换这种方法目前还好用的 /** * Check if the element present with cu ...

  7. VueX-状态管理器

    一.VueX功能与解决的问题 1.中央状态管理器的功能: 1.1.可以管理共享状态1.2.提供一 个可修改状态的方法1.3.提供状态获取的方法1.4.状态更改后,有通知机制 2.中央状态管理器解决的问 ...

  8. CodeForces 572D Minimization(DP)

    题意翻译 给定数组AAA 和值kkk ,你可以重排AAA 中的元素,使得∑i=1n−k∣Ai−Ai+k∣\displaystyle\sum_{i=1}^{n-k} |A_i-A_{i+k}|i=1∑n ...

  9. handsontable-utilities

    搜索值 鼠标右键 讲了四个功能:1.row header是否可以右键(rowheader:true):2.删除右键列表的某些值(通过数组定义):3.自定义右键列表和功能(callback,item两个 ...

  10. 我的Jquery参考词典

    由于工作主要用到Asp.net Mvc+Jquery,最近也看了一些Jquery的书籍,在此总结以备回顾. 已读书籍:<Jquery In Action> 主要讲了些Jquery语法以及A ...