您的位置:首页 > 其它

【strstr】#23 A. You're Given a String...

2014-05-30 15:29 399 查看
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
0


input
ababa


output
3


input
zzz


output
2


本来吓了一跳……这是DP么……就算是我也不会呀……只能O(n^3)啊……

一看数据……100……

额……直接暴力了…… 这次用的是 strstr(Momstring,Kidstring,Kidlength)函数(返回第一次找到的指针),挺好用的~~

#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>

/*************************************************
strstr
原型:const char * strstr ( const char * str1, cosnt char *str2);
            char * strstr ( char * str1, const char * str2);
参数:	str1,待查找的字符串指针;
		str2,要查找的字符串指针。
说明:	在str1中查找匹配str2的子串,并返回指向首次匹配时的第一个元素指针。
		如果没有找到,返回NULL指针。
***************************************************/

using namespace std;
int main()
{
	//freopen("in.txt","r",stdin);
	char line[101];
	char find[100];
	scanf("%s",line);
	int len=strlen(line);
	for(int l=len-1;l>0;l--)
	{
		for(int i=0;i<=len-l;i++)
		{
			strncpy(find,line+i,l);//从line里的第i位开始,数l个字符的字符串扔给find
			find[l]='\0';//很重要,不封口简直是作死
			if(strstr(strstr(line,find)+1,find))//找到一个,然后在他后头一位开始找第二个
			{
				printf("%d",l);
				return 0;
			} 
		}
	}
	printf("0");
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: