- #include<iostream>
- #include<string>
- #include<cstring>
- using namespace std;//s 的第一个字符和 t 比较, 不相等就和第二个比较, 如果一旦有不相等的就继续比较, 设置一个标记, 如果一直没有就更新一下 s 的顺序
- void change(string &s,int &count)
- {
- int length;
- length = s.size();
- char a = s[0];
- for (int i = 1; i <length; i++)
- {
- s[i - 1] = s[i];
- }
- s[length - 1] = a;
- count++;
- }
- bool Judge(string s, string t)
- {
- int flag=0;
- int count=0;// 当 count 比 s 的长度大的时候, 如果依然没找到就输出 no;
- int length = s.size();
- int ll = t.size();
- while (1)
- {
- int i=0, j = 0;
- int tt = 0;
- while (j!=ll)
- {
- if (s[i] == t[j])
- {
- i++;
- j++;
- if (i-1== 0)
- {
- tt = j-1;
- continue;
- }
- if (i-1== length - 1)
- {
- flag = 1;
- break;
- }
- }
- else
- if (s[i] != t[j])
- {
- if (i == 0)
- {
- j++;
- continue;
- }
- else
- if (i != 0)
- {
- i = 0;
- j = tt + 1;
- continue;
- }
- }
- }
- if (flag == 1)
- break;
- else
- if (count < length)
- {
- change(s, count);
- }
- else
- break;
- }
- if (flag == 1)
- return true;
- else
- return false;
- }
- int main()
- {
- string s, t;
- while (cin>> s>> t && (s != "0" || t != "0"))
- {
- if (Judge(s, t))
- cout << "YES" << endl;
- else
- cout << "NO" << endl;
- }
- return 0;
- }
错误原因:
1, 一开始循环弄错了, 导致死循环
2,count 没有加上引用符, 还是导致死循环了
思路:
1, 首先写一个子函数用来改变病毒字符串的顺序
2,flag 标记是否有相同的
3, 有过病毒字符串有连续几个和人的一样, 但下一个不一样, 需要一个 tt 来记住第一个 i 相等的时候, j 的大小
来源: http://www.bubuko.com/infodetail-3266074.html