β

CodeForces 23A You’re Given a String…

Dianlujitao 139 阅读
acm
A. You're Given a String…
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You're given a string of lower-case Latin letters. Your task is to find the length of its longest substring that can be met in the string at least twice. These occurrences can overlap (see sample test 2).

Input

The first input line contains the string. It's guaranteed, that the string is non-empty, consists of lower-case Latin letters, and its length doesn't exceed 100.

Output

Output one number — length of the longest substring that can be met in the string at least twice.

Sample test(s)
input
abcd

output

input
ababa

output

input
zzz

output

爆搜O(n^2)复杂度,完全可以接受

#include <iostream>
#include <string>
using namespace std;
string s;
int len;
int ans, tmp;
void input()
{
	cin >> s;
}
void solve()
{
	len = s.length();
	for (int i = 0; i < len; i++)
	{
		for (int j = i + 1; j < len; j++)
		{
			tmp = 0;
			while (s[i + tmp] == s[j + tmp])
				tmp++;
			if (tmp > ans)
				ans = tmp;
		}
	}
	cout << ans << "\n";
}
int main()
{
	input();
	solve();
	return 0;
}
acm
作者:Dianlujitao
只有面对现实,才能超越现实
原文地址:CodeForces 23A You’re Given a String…, 感谢原作者分享。