7.8 科大讯飞笔试 编程第三题 好串
小红定义一个字符串是“好串”,当且仅当该字符串的长度不小于2,且首尾相同。例如"arcaea"是好串。
小红拿到了一个字符串(该字符串不一定是好串),她准备把这个字符串切割成若干个好串,你可以帮小红求出好串的最多数量吗?
输入描述
一个仅包含小写字母的字符串,长度不超过200000。
输出描述
如果无法切割且该字符串本身不是好串,请输出-1。否则输出最终的好串数量。
------------------------------------------------------
用了滑动窗口+贪心,但0%。。。
string s;
cin >> s;
if (s.size() <= 1)
{
cout << -1;
return 0;
}
int count = 0;
bool meethead = false;
for (int i = 0; i < s.size(); ++i)
{
char head = s[i++];
meethead = false;
while (i < s.size() && !meethead)
{
if (s[i] == head)
{
meethead = true;
break;
}
++i;
}
if (meethead)
++count;
}
if (count > 0)
cout << count;
else
cout << -1;
return 0;


