您的位置:首页 > 其它

hdu 1403 Longest Common Substring(求公共子串长度)

2015-08-14 11:33 363 查看

Longest Common Substring

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5011    Accepted Submission(s): 1745


[align=left]Problem Description[/align]
Given two strings, you have to tell the length of the Longest Common Substring of them.

For example:

str1 = banana

str2 = cianaic

So the Longest Common Substring is "ana", and the length is 3.

 

[align=left]Input[/align]
The input contains several test cases. Each test case contains two strings, each string will have at most 100000 characters. All the characters are in lower-case.

Process to the end of file.

 

[align=left]Output[/align]
For each test case, you have to tell the length of the Longest Common Substring of them.

 

[align=left]Sample Input[/align]

banana
cianaic

 

[align=left]Sample Output[/align]

3

 

[align=left]Author[/align]
Ignatius.L
 

[align=left]Recommend[/align]
We have carefully selected several similar problems for you:  1404 1418 1422 1409 1427 
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<iostream>
#include<string.h>
using namespace std;
#define N 100100
struct note{
char *vis;
int map;
}s[N*2];
char a
,b
;
int cmp(const void *a,const void *b)
{
return strcmp(((note *)a)->vis,((note *)b)->vis);
}
int fun(char *a,char *b)
{
int len=0;
while(*a++==*b++)
len++;
return len;
}
int main()
{
int str1,str2,i;
while(~scanf("%s%s",&a,&b))
{
str1=strlen(a);
str2=strlen(b);
for(i=0;i<str1;i++)
{
s[i].vis=a+i;
s[i].map=1;
}
for(i=0;i<str2;i++)
{
s[i+str1].vis=b+i;
s[i+str1].map=-1;
}
int max,tem;
max=0;
qsort(s,str1+str2,sizeof(note),cmp);
for(i=0;i<str1+str2-1;i++)
{
if(s[i].map!=s[i+1].map&&(tem=fun(s[i].vis,s[i+1].vis))>max)
max=tem;
}
printf("%d\n",max);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: