当前位置:网站首页>776. 字符串移位包含问题
776. 字符串移位包含问题
2022-07-28 20:50:00 【Hunter_Kevin】
题目
对于一个字符串来说,定义一次循环移位操作为:将字符串的第一个字符移动到末尾形成新的字符串。
给定两个字符串 s1 和 s2,要求判定其中一个字符串是否是另一字符串通过若干次循环移位后的新字符串的子串。
例如 CDAA 是由 AABCD 两次移位后产生的新串 BCDAA 的子串,而 ABCD 与 ACBD 则不能通过多次移位来得到其中一个字符串是新串的子串。
输入格式
共一行,包含两个字符串,中间由单个空格隔开。
字符串只包含字母和数字,长度不超过 30。
输出格式
如果一个字符串是另一字符串通过若干次循环移位产生的新串的子串,则输出 true,否则输出 false。
输入样例:
AABCD CDAA
输出样例:
true
思路
手写kmp算法,查找字串问题,将字符串复制拼接成双倍再进行字串查找即可相等于移位操作
代码
#include <iostream>
#include <cstring>
using namespace std;
const int N = 100;
void get_next(char T[], int ne[])
{
int n = strlen(T);
ne[1] = 0;
for(int i = 2, j = 0; i <= n; i++)
{
while(j && T[i] != T[j+1]) j = ne[j];
if(T[i] == T[j+1]) j++;
ne[i] = j;
}
}
bool kmp(char T[], char S[])
{
int ne[N] = {
0};
get_next(T, ne);
int n = strlen(T+1);
int m = strlen(S+1);
for(int i = 1, j = 0; i <= m; i++)
{
while(j && S[i] != T[j+1]) j = ne[j];
if(S[i] == T[j+1]) j++;
if(j == n)
return true;
}
return false;
}
int main()
{
char S1[N], S2[N];
cin >> S1+1 >> S2+1;
char T1[N], T2[N];//临时保存S+1 和 S2+1 ,后面字符串匹配都会作为字串, T1+1跟双倍后的S2+1进行匹配,T2+1跟双倍后的S1+1进行匹配
strcpy(T1+1,S1+1);//S1临时存在T1,而S1双倍
strcat(S1+1,T1+1);
strcpy(T2+1,S2+1);//S2临时存在T2,而S2双倍
strcat(S2+1,T2+1);
//任意一个字串匹配成功即可
if (kmp(T1, S2) || kmp(T2, S1))puts("true");
else puts("false");
return 0;
}
边栏推荐
- 纪念一下第一次写的线段树了喽(对应洛谷3372)
- Kali source solution software cannot be installed correctly
- Establishment of Ruiji takeout development environment
- Why doesn't the icon on the elment plus icon input display
- PaddleNLP基于ERNIR3.0文本分类以中医疗搜索检索词意图分类(KUAKE-QIC)为例【多分类(单标签)】
- 32. Longest valid bracket (difficult stack string)
- Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)
- ssh免密登陆
- redis相关
- tutorial/detailed_ workflow. Ipynb quantitative finance qlib Library
猜你喜欢

示波器发展史中的变化

If you want to grow rapidly, you must first experience a major blow!

Ruiji takeout project - development of business development function Day2

Win11怎么打开软件通知

HCIP(8)

Hcip experiment (14)
How do we do full link grayscale on the database?

网易云信 2022Q2 产品补给站,快来获取你的产品补给计划吧!

Win11 how to open software notification

Netease Yunxin 2022q2 product supply station, come and get your product supply plan!
随机推荐
Sword finger offer II 052. flatten binary search tree (simple binary search tree DFS)
internet的基本服务中文件传输命令是哪个
[Ruiji takeout project]day4 - dish management
Container configuration starts redis cluster single machine 6 nodes 3 Master 3 slave
105. Construct binary tree from preorder and inorder traversal sequence (medium binary tree DFS hash table binary tree)
Att & CK preliminary understanding
The blueprint of flask complements openpyxl
[CS231N]Lecture_ 2:Image Classification pipelin
HCIP(11)
Day3 classification management of Ruiji takeout project
【二叉树】二叉树中的伪回文路径
Remember the first line segment tree (corresponding to Luogu 3372)
LCR测试仪最为主要的功能和用途都是什么
20-09-27 the project is migrated to Alibaba toss record (the network card order makes the service unable to connect to DB through haproxy)
How about the actual use effect of common source oscilloscope
2022年一级建造师考试什么时候才能报名?
The binary search boundary value processing based on leetcode35 is used to clarify the boundary value of the judgment condition using the idea of interval
示波器发展史中的变化
Sword finger offer II 067. maximum XOR (medium prefix tree bit operation array)
gprs网络指的是什么