problem

686. Repeated String Match

solution1:

使用string类的find函数;

class Solution {
public:
int repeatedStringMatch(string A, string B) {
int n1 = A.size(), n2 = B.size(), cnt = ;
string t = A;
while(t.size() < n2)
{
t += A;
cnt++;
}
if(t.find(B) != string::npos) return cnt;//err.
t += A;
return (t.find(B) != string::npos) ? cnt+ : -;//
}
};

solution2:

其实可以直接算出最多需要增加的个数,即B的长度除以A的长度再加上2,当B小于A的时候,那么可能需要两个A,所以i就是小于等于2,这样每次在t中找B,如果找到了,就返回i,没找到,就增加一个A,循环推出后返回-1即可。

参考

1. Leetcode_easy_686. Repeated String Match;

2. Grandyang;

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

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

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

  2. 【Leetcode_easy】942. DI String Match

    problem 942. DI String Match 参考 1. Leetcode_easy_942. DI String Match; 完

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

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

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

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

  7. 686. Repeated String Match

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

  8. 【Leetcode_easy】844. Backspace String Compare

    problem 844. Backspace String Compare solution1: class Solution { public: bool backspaceCompare(stri ...

  9. 【Leetcode_easy】796. Rotate String

    problem 796. Rotate String solution1: class Solution { public: bool rotateString(string A, string B) ...

随机推荐

  1. Mybatis的简单搭建

    1.官方网址 http://www.mybatis.org/mybatis-3/zh/getting-started.html 2.导入jar包 3.根据官方文档,首先写mybatis-config. ...

  2. Web前端 --- BOM和DOM

    目录 前端基础之BOM和DOM BOM DOM 前端基础之BOM和DOM BOM是指浏览器对象模型,他使JavaScript有能力与浏览器进行对话 DOM是指文档对象模型,通过它,可以访问HTML文档 ...

  3. sql server 常见约束

    1.not null 非空约束 ①强制列不接受空值 ②例:创建表时,name varchar(6) not null, 2.unique 唯一性约束 ①约束唯一标识数据库表中的每条记录 ②unique ...

  4. ES WIndows 安装 ES与ES-head

    一.ES的安装 1.到ES官网下载ES 安装ES前,需要安装JDK1.8以上版本 https://www.elastic.co/downloads/elasticsearch 2.解压ES 3.安装E ...

  5. Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1)

    Virtual participate 的,D题不会做,打了1:30就打不动了,过了ABCE. A - CME 题意:? 题解:? void test_case() { int n; scanf(&q ...

  6. [信息收集]Nmap命令详解

    0x00[介绍] Nmap,也就是Network Mapper,中文为"网络映射器". Nmap是一款开源的网络探测和安全审核的工具,它的设计目标是快速地扫描大型网络. 它是网络管 ...

  7. BZOJ4237稻草人——单调栈+CDQ分治

    题目描述 JOI村有一片荒地,上面竖着N个稻草人,村民们每年多次在稻草人们的周围举行祭典. 有一次,JOI村的村长听到了稻草人们的启示,计划在荒地中开垦一片田地.和启示中的一样,田地需要满足以下条件: ...

  8. 如何设置fvOptions【翻译】

    翻译自:CFD-online 帖子地址:http://www.cfd-online.com/Forums/openfoam-pre-processing/121763-how-set-fvoption ...

  9. Redis企业实战的一些坑

    附录:Redis企业实战的一些坑 一.前言 小伙伴们对Redis应该不陌生,Redis是系统必备的分布式缓存中间件,主要用来解决高并发下分担DB资源的负载,从而提升系统吞吐量. Redis支持多种数据 ...

  10. Collections用法总结

    Collections是一个包装类,其中包含有各种有关集合操作的静态多态方,比如可以作用在List和Set上,此类不能实例化. 排序Integer[] array = new Integer[]{3, ...