-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path776chatgpt.cpp
More file actions
57 lines (51 loc) · 1.52 KB
/
776chatgpt.cpp
File metadata and controls
57 lines (51 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool is_substring(const string &str1, const string &str2)
{
if (str1.empty() || str2.empty() || str1.length() > str2.length())
return false;
for (int i = 0; i <= str2.length() - str1.length(); ++i)
{
int j = 0;
for (; j < str1.length(); ++j)
{
if (str2[i + j] != str1[j])
break;
}
if (j == str1.length())
return true;
}
return false;
}
int main()
{
string str1, str2;
cout << "Enter the first string: ";
cin >> str1;
cout << "Enter the second string: ";
cin >> str2;
if (str1.length() < str2.length())
swap(str1, str2);
int len = str1.length();
for (int n = 0; n < len; n++)
{
// n = n % len;
// if (n < 0)
// n += len;
// cout << (str1.substr(len - n) + str1.substr(0, len - n)) << endl;
cout << (str1.substr(len - n, n) + str1.substr(0, len - n)) << endl;
// substr复制子字符串,要求从指定位置开始,并具有指定的长度。将前后两个短字符串拼接在一起
string str3 = (str1.substr(len - n, n) + str1.substr(0, len - n));
if (is_substring(str2, str3))
{
cout << str2 << " is a substring of " << str1 << endl;
}
else
{
cout << str2 << " is not a substring of " << str1 << endl;
}
}
return 0;
}