【刷题笔记】686. Repeated String Match
题意
题目大意是,给两个字符串 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的更多相关文章
- 【Leetcode_easy】686. Repeated String Match
problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...
- 【LeetCode】686. Repeated String Match 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 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 ...
- 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 ...
- [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 ...
- 686. Repeated String Match
方法一.算是暴力解法吧,拼一段找一下 static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); cl ...
- 【leetcode刷题笔记】Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...
- 【leetcode刷题笔记】Scramble String
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- 686. Repeated String Match判断字符串重复几次可以包含另外一个
public static int repeatedStringMatch(String A, String B) { //判断字符串a重复几次可以包含另外一个字符串b,就是不断叠加字符串a直到长度大 ...
随机推荐
- centos下Linux C语言MD5的使用
在Linux C变成中用到MD5加密会使用到openssl库,下面给出的是一个简单的小Demo: #include <stdio.h> #include <openssl/md5.h ...
- Linux mysql 乱码
http://www.pc6.com/infoview/Article_63586.html http://itindex.net/detail/41748-linux-mysql-5.5 http: ...
- spring bean-- autowired的正确用法
这两天用idea写spring注入的时候每一次 @Autowired Worker worker; 都会报黄,用过这个ide的都知道,说明你代码需要重构了. 然后提示的信息是 Spring Team ...
- standard_key.kmp
[KeyRemap]keyVersion=2B33554467=[eraseeof]S36=[bof]B33554466=[pagedn]S35=[eof]B33554465=[pageup]B10= ...
- [USACO06FEB]摊位预订Stall Reservations(贪心)
[USACO06FEB]摊位预订Stall Reservations 题目描述 Oh those picky N (1 <= N <= 50,000) cows! They are so ...
- Django--Auth模块使用
1.Auth模块介绍 1.1 Auth模块是Django自带的用户认证模块,用于处理用户账户.群组.许可和基于cookie的用户回话 Django的认证系统主要包括下面几个部分 1.用户 2.许可 3 ...
- SDOI2019R2翻车记
额...貌似是学OI以来翻得最惨的一次比赛了呢... 不过还好是初三 但是没有机会和学长们打最后一场告别赛了呢(笑 按照惯例还是要记录一下吧. DAY ? 中考倒计时30天.来写这篇游记. DAY 0 ...
- ThreadPoolExecutor扩展
import java.util.concurrent.*; /** * ThreadPoolExecutor扩展 */ public class ExtThreadPool { public sta ...
- Android Lint Problem
问题概述: Type: Android Lint Problem 解决方法: select problems -> quick fix-> Clear Lint Markers
- 排查Java高CPU占用原因
近期java应用,CPU使用率一直很高,经常达到100%,通过以下步骤完美解决,分享一下. 方法一: 转载:http://www.linuxhot.com/java-cpu-used-high.htm ...