题解 CF23A 【You're Given a String...】
Update:2021.1.14,发现了几个错误
暴力就完了!
这里我们很明显可以用接近 的暴力枚举,那么对于匹配两次出现我们可以用 find。
每次 erase?
不对,erase 的坏处是会直接破坏原来字符串,并且也不是 的删除,我们可以用 find 的另一个重载函数去做:
find(string, int);
第一个还是存要找的字符串,第二个是一个下标,代表搜索范围从这个下标到最后一个字符。
代码,996ms:
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
inline bool check(string&);
string s;
int main()
{
cin >> s;
int len = s.size() - 1, ans = 0;
for (register int i = len; i >= 0; i--)
{
for (register int j = 0; j <= i; j++)
{
string x = s.substr(j, i - j + 1);
if (check(x))
{
ans = max(ans, i - j + 1);
}
}
}
cout << ans << endl;
system("pause");
return 0;
}
inline bool check(string& x)
{
string v = s;
int cnt = 0, last = -1;
while (v.find(x, last + 1) != string::npos && cnt < 2)
{
cnt++;
last = v.find(x, last + 1);
}
return cnt >= 2;
}

浙公网安备 33010602011771号