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

来源:互联网 发布:淘宝客推广位是什么 编辑:程序博客网 时间:2024/04/28 02:40

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


Problem Description
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.
 

Input
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.
 

Output
For each test case, you have to tell the length of the Longest Common Substring of them.
 

Sample Input
bananacianaic
 

Sample Output
3
 

Author
Ignatius.L
 

Recommend
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 100100struct note{char *vis;int map;}s[N*2];char a[N],b[N];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;}

 
0 0
原创粉丝点击