当前位置:网站首页>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;
}
边栏推荐
- HCIP(14)
- CMD common commands
- Sword finger offer II 052. flatten binary search tree (simple binary search tree DFS)
- mysql8.0无法给用户授权或提示You are not allowed to create a user with GRANT的问题
- Closure, prototype and original link
- LeetCode刷题系列之-多数之和类型
- 普源示波器实际的使用效果怎么样
- Why doesn't the icon on the elment plus icon input display
- Sword finger offer II 067. maximum XOR (medium prefix tree bit operation array)
- JS array merging, de duplication, dimensionality reduction (es6: extended operator, set)
猜你喜欢

HCIP(14)

MySQL built-in functions
Integrating database Ecology: using eventbridge to build CDC applications

Sword finger offer II 055. Binary search tree iterator (medium binary search tree iterator)

Static route and default route experiment

Ultra detailed visual studio 2019 running littlevgl (lvgl) simulator

想要快速成长,先要经历重大打击!

什么是时间复杂度

静态路由和缺省路由实验

Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)
随机推荐
Att & CK preliminary understanding
Log4j vulnerability elk platform processing method (logstah5.5.1)
mysql8.0无法给用户授权或提示You are not allowed to create a user with GRANT的问题
Common commands of NPM
PaddleNLP基于ERNIR3.0文本分类以中医疗搜索检索词意图分类(KUAKE-QIC)为例【多分类(单标签)】
Establishment of Ruiji takeout development environment
微信小程序剪切图片的功能
LeetCode刷题系列之-多数之和类型
gprs网络指的是什么
MySQL installation and configuration (super detailed, simple and practical)
2021 mathematical modeling group B exercise
ssh 免密码登录
Use REM to make the font size adaptive to the screen
使用webWorker执行后台任务
成立不到一年!MIT衍生量子计算公司完成900万美元融资
Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million
HCIP(13)
SSH password free login
Sword finger offer II 064. magic Dictionary (medium dictionary tree string design)
CDN working principle