字符串移位包含问题-题解

描述

对于一个字符串来说,定义一次循环移位操作为:将字符串的第一个字符移动到末尾形成新的字符串。

给定两个字符串s1和s2,要求判定其中一个字符串是否是另一字符串通过若干次循环移位后的新字符串的子串。例如CDAA是由AABCD两次移位后产生的新串BCDAA的子串,而ABCD与ACBD则不能通过多次移位来得到其中一个字符串是新串的子串。

输入描述

一行,包含两个字符串,中间由单个空格隔开。每个字符串只包含字母和数字,长度不超过100。

输出描述

如果一个字符串是另一字符串通过若干次循环移位产生的新串的子串,则输出true,否则输出false。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;

bool x(string a, string b) {
string temp = a + a;
return (temp.find(b) != string::npos);
}

int main() {
string a, b;
cin >> a >> b;
if (x(a, b)) {
cout << "true";
} else {
cout << "false";
}
return 0;
}

字符串移位包含问题-题解
https://chenxi-tijie.pages.dev/2025/07/字符串移位包含问题-题解/
作者
chenxi
发布于
2025年7月4日
许可协议