当前位置:网站首页>776. String shift inclusion problem
776. String shift inclusion problem
2022-07-28 22:38:00 【Hunter_ Kevin】
subject
For a string , Define a cyclic shift operation as : Move the first character of a string to the end to form a new string .
Given two strings s1 and s2, It is required to determine whether one of the strings is a substring of the new string after the other string has been circularly shifted for several times .
for example CDAA By AABCD A new string produced after two shifts BCDAA The string of , and ABCD And ACBD You can't get that one of the strings is a substring of the new string through multiple shifts .
Input format
All in one line , Contains two strings , Separated by a single space .
The string contains only letters and numbers , Length not exceeding 30.
Output format
If a string is a substring of a new string generated by several cyclic shifts of another string , The output true, Otherwise output false.
sample input :
AABCD CDAA
sample output :
true
Ideas
Handwriting kmp Algorithm , Find String Problems , Copy and splice the string into double and then search the string, which is equivalent to the shift operation
Code
#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];// Temporary storage S+1 and S2+1 , The following string matches will be used as a string , T1+1 With the double S2+1 Match ,T2+1 With the double S1+1 Match
strcpy(T1+1,S1+1);//S1 Temporary presence T1, and S1 double
strcat(S1+1,T1+1);
strcpy(T2+1,S2+1);//S2 Temporary presence T2, and S2 double
strcat(S2+1,T2+1);
// Any string matches successfully
if (kmp(T1, S2) || kmp(T2, S1))puts("true");
else puts("false");
return 0;
}
边栏推荐
- [binary tree] pseudo palindrome path in binary tree
- Integrating database Ecology: using eventbridge to build CDC applications
- For loops and functions
- Excel-vba quick start (XIII. Common usage of date)
- PaddleNLP基于ERNIR3.0文本分类:WOS数据集为例(层次分类)
- [leetcode] maximum depth of binary tree
- Ruiji takeout project - development of business development function Day2
- Solve the problem that TS node xxx.ts executes TS code and reports errors
- Solve various problems of sudo rosdep init and rosdep update
- Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million
猜你喜欢
随机推荐
[virtual machine _2]-hyper-v and vmware/virtualbox cannot coexist
近期bug总结
[connect your mobile phone wirelessly] - debug your mobile device wirelessly via LAN
Solve the problem that TS node xxx.ts executes TS code and reports errors
Awk blank line filtering
Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)
删除容器镜像报错解决image is referenced in multiple repositories
What is time complexity
Winserver operation and maintenance technology stack
微信小程序里button点击的时候会边框有黑线
纪念一下第一次写的线段树了喽(对应洛谷3372)
Static route and default route experiment
76. Minimum coverage substring (hard sliding window hash table string)
Log4j vulnerability elk platform processing method (logstah5.5.1)
96. Different binary search trees (medium binary search tree dynamic planning)
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)
NPM switch Taobao source (NPM source)
Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million
Soft exam network engineer
容器化配置启动redis集群 单机6节点 3主3从









