题意

题目大意是,给两个字符串 A 和 B,问 B 是否能成为 A+A+A+...+A 的子字符串,如果能的话,那么最少需要多少个 A?

暴力解法

直接 A+A+...,到哪次 A 包含 B 了,就返回 A 的个数。

但是 B 也可能不是 A 的拼接的子字符串,所以这种直观解法还是存在隐患(无限循环),最好还是动动脑筋。

动脑筋解法

假如 B 的长度为 b,A 的长度为 a,那么 n=Math.ceil(b/a) 一定意味着什么。但是到底 n 意味着什么呢?看看例子先。

假设 A=“abcdefg”, B="gab", n=Math.ceil(3/7) = 1, 这种情况下,B 是 A+A 的子字符串,也就是答案为 2,为 n+1

假设 A=“ab”,B="abababa",n=Math.ceil(7/2) = 4,这种情况下,B 是 A+A+A+A 的子字符串,也就是答案为 4,为 n

从上面两种情况看,答案通常为 n 或者 n+1。但是这两种情况是否涵盖了所有的情况呢?我们无法肯定。

我们可以提出假设,假设最终答案最小为 n(这一点很容易证明,不然 B 比 A 长,哪里能成为 A 的子字符串),最大为 n+1

那么我们只需要证明第二点,通过反推,证明 答案大于等于 n+2 的时候,存在累赘的 A  就可以了。

如下图,假设 B 的长度是 A 的长度的 n 倍小一点。

如果答案为 n+2 的话,那么 B 在 A 的拼接字符串中的匹配情况应该如下图所示:

或者是

第一种情况,最前面那个 A 是多余的;第二种情况,最后面那个 A 是多余的。由此可以得出,如果存在答案,答案不会超过 n+1。

结论

这道题答案存在三种情况,假设 n = Math.ceil( B.length/A.length ),则

1. A 怎么拼接都不可能成为 B 的母串;

2. 答案为 n;

3. 答案为 n+1。

代码就不提供了,按照这种思路,很容易就可以解答出来的。

【刷题笔记】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. 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 ...

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

  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

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

  7. 【leetcode刷题笔记】Interleaving String

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...

  8. 【leetcode刷题笔记】Scramble String

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

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

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

随机推荐

  1. mysql数据库操作指令汇总

    1.mysql -u root -p 登录数据库 2.表结构相同两表数据对拷 insert into A select * from B(插入全部字段数据)   insert into A(字段1.字 ...

  2. BUUCTF--刮开有奖

    文件链接:https://buuoj.cn/files/abe6e2152471e1e1cbd9e5c0cae95d29/8f80610b-8701-4c7f-ad60-63861a558a5b.ex ...

  3. Cheatsheet: 2019 03.01 ~ 04.30

    Golang How To Install Go and Set Up a Local Programming Environment on macOS Build A Go API 40+ prac ...

  4. 一、asp的写法

    一.asp的写法  vs从来都不支持asp,但是可以用vscode写,好多年前写asp的时候,用的是dreamwaver,asp还有创建项目这一说法?调试搭个iis就行了   <html> ...

  5. HDU 6613 Squrirrel 树形dp

    题意:给你一颗树,你可以把这棵树上的一条边的边权变为0,现在让你选一个根,让所有点到这个点的最大距离尽量的小.如果有多个根的最大距离距离相同,输出编号最小的边. 思路:如果没有把边权变为0的操作,这个 ...

  6. ini配置文件内如果有更新参数

    ini配置文件内如果有更新参数 执行更新 更新参数 自动去下载执行????

  7. ssm科普篇

    springMVC执行步骤: 1.用户发送请求到前端控制器,前端控制器根据请求信息来决定选择页面控制器,并将请求委托给它 2.页面控制器收到请求后,进行功能处理,首先需要收集和绑定请求参数到一个对象, ...

  8. hibernate Criteria(条件查询接口)

    Criteria(条件查询接口) // 1.简单查询 List<Customer> list = session.createCriteria(Customer.class).list() ...

  9. 前端每日实战:50# 视频演示如何用纯 CSS 创作一个永动的牛顿摆

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/qKmGaJ 可交互视频教程 此视频 ...

  10. python读文件的4种方式

    1.直接打开就读 with open('filepath','r') as f: for line in f: print(line) print('一行数据') 虽然f是一个文件实例,但可以通过以上 ...