- 字符串移位包含问题
对于一个字符串来说,定义一次循环移位操作为:将字符串的第一个字符移动到末尾形成新的字符串。
给定两个字符串s1和s2,要求判定其中一个字符串是否是另一字符串通过若干次循环移位后的新字符串的子串。例如CDAA是由AABCD两次移位后产生的新串BCDAA的子串,而ABCD与ACBD则不能通过多次移位来得到其中一个字符串是新串的子串。
- Input
一行,包含两个字符串,中间由单个空格隔开。字符串只包含字母和数字,长度不超过30。
- Output
如果一个字符串是另一字符串通过若干次循环移位产生的新串的子串,则输出true,否则输出false。
- Sample Input
AABCD CDAA
- Sample Output
true
虽然题目简单,但还是要注意判断两个字符的长度,因为并不知道子串是哪个
第一种解法是模拟,就不说了,直接看代码,自己体会吧
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
char str1[31], str2[31], tmp[31];
scanf("%s %s", str1, str2);
int s1 = strlen(str1);
int s2 = strlen(str2);
if(s1 < s2){
int x;
tmp[31] = str1[31];
str1[31] = str2[31];
str2[31] = tmp[31];
x=s1;
s1=s2;
s2=x;
}
int i, j;
for(i=0; i < s1; i++){