首页 > 其他分享 >AcWing 776. 字符串移位包含问题

AcWing 776. 字符串移位包含问题

时间:2023-05-09 10:03:52浏览次数:32  
标签:776 s1 字符串 include 移位 AcWing

AcWing 776. 字符串移位包含问题


1. 地址

    https://www.acwing.com/problem/content/778/

2. 题解

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>

using namespace std;

int main(){
    string s;
    string s1;
    cin >> s;
    cin >> s1;
    if(s.size() < s1.size()){
        swap(s,s1);
    }
    //移位
    for(int i=0;i<s.size();i++){
        //主串进行移位
        //需要注意:string的substr()方法代表取子串,substr(1)代表取从下标1到结尾的子串
        s = s.substr(1) + s[0];
        //遍历起点
        for(int j=0;j + s1.size() <= s.size();j++){
            int k;
            //遍历子串
            for(k=0;k<s1.size();k++){
                if(s[j+k] != s1[k]){
                    break;
                }
            }
            if(k == s1.size()){
                puts("true");
                return 0;
            }
        }
    }
    puts("false");
    return 0;
}
//本题可以进行优化,使用kmp算法

标签:776,s1,字符串,include,移位,AcWing
From: https://www.cnblogs.com/gao79135/p/17384008.html

相关文章

  • AcWing 771. 字符串中最长的连续出现的字符
    AcWing771.字符串中最长的连续出现的字符1.地址https://www.acwing.com/problem/content/description/773/2.题解#include<iostream>#include<cstdio>#include<string>usingnamespacestd;intmain(){intn;intsum;intmax;int......
  • Acwing周赛102
    倍增这是一道简单数论题usingnamespacestd;typedeflonglongLL;constintN=1e5+10;inta[N],n;intdiv(intx){if(x%2==0)while(x%2==0)x/=2;if(x%3==0)while(x%3==0)x/=3;returnx;}intma......
  • workerman下框架gateway报错 worker[thinkphp:30776] exit with status 64000
    wokerman启动之后一直报错Worker[30477]processterminatedworker[thinkphp:30477]exitwithstatus64000Worker[30533]processterminatedworker[thinkphp:30533]exitwithstatus64000Worker[30571]processterminatedworker[thinkphp:30571]exitwithstatus64......
  • AcWing 770. 单词替换
    AcWing770.单词替换1.地址https://www.acwing.com/problem/content/772/2.题解#include<iostream>#include<cstdio>#include<sstream>usingnamespacestd;intmain(){strings;stringa,b;stringresult="";......
  • 【算法基础】DFS深度优先算法 —— AcWing 843. n-皇后问题 AcWing 842. 排列数字
    n-皇后问题是一个经典的dfs深度优先遍历的题目,在题解这一题之前,将由浅入深,先讲解一个n-皇后问题的母题。-------AcWing842.排列数字 [AcWing842].排列数字题目概述给定一个整数 n,将数字 1∼n排成一排,将会有很多种排列方法。现在,请你按照字典序将所有的排列方法输出。输入格......
  • POJ2739 Sum of Consecutive Prime Numbers&&Acwing4938 连续质数之和
    方法:单调队列为什么是单调队列?因为这里让我们求连续的质数和,我们可以利用欧拉筛来维护质数,再利用单调队列来维护连续的质数。代码(POJ不支持C++11差评):#include<cstdlib>#include<cstring>#include<cstdio>#include<cctype>namespaceFastIo{ #definegcgetchar() #d......
  • acwing 4645. 选数异或
     输出yesnoyes no题意分析,给一串数组,再在每次提问时给出一个区间,l,r;求l,r区间内是否存在两个数,两数异或后值为给出的x;已知a^b=x-->a^x=b;思路:1,把每个数异或x,存在另一个数组(b)里,暴力搜索,看区间内b数组内数字是否有等于a数组内数字,TLE2.记录下标,比较每个......
  • AcWing 3549. 最长非递减子序列
    \(AcWing\)\(3549\).最长非递减子序列一、题目描述给定一个长度为\(n\)的数字序列\(a_1,a_2,…,a_n\),序列中只包含数字\(1\)和\(2\)。现在,你要选取一个区间\([l,r](1≤l≤r≤n)\),将\(a_l,a_{l+1},…,a_r\)进行翻转,并且使得到的新数字序列\(a\)的最长非递减子序列......
  • AcWing 1209. 带分数
    1-暴力解法思考1:暴力列举出1~9的全排列,之后再将这些数字按照一定规则相加,最后将结果与n比较。全排列好写,但相加的规则不好写,而且太暴力了,估计会超时。/*AcWing1209.带分数00.最暴力的写法1.枚举全排列2.枚举位数(枚举a和b,可算出c)3.直接算出n,判断等......
  • AcWing 754. 平方矩阵 II
    AcWing754.平方矩阵II1.地址https://www.acwing.com/problem/content/756/2.题解#include<iostream>#include<cstdio>#include<cmath>usingnamespacestd;//每个元素的值为:各个元素下标相减的绝对值+1intmain(){intmatrix[102][102];intn;......