5 longest common string (100)
Given two strings a, b, k prior chance in a string of characters to be modified, so that the longest common substring modified two longest string. Each modification can choose a, an arbitrary position in a string b modify an arbitrary character string.
Input formats:
The first line includes a positive integer k.
The second and third rows, respectively of the input string a, b. (The length of each string does not exceed 500)
Output formats:
Output is an integer representing the length of the longest common substring of two strings modified.
Sample input:
5
aaaaa
bbbbb
Sample output:
5
Accepted
#include <stdio.h>
#include <string.h>
int main()
{
int k,i,j,len1,len2,t1,t2,sum,time,max=-1;
char s1[505],s2[505];
scanf("%d",&k);
scanf("%s",&s1);
scanf("%s",&s2);
len1=strlen(s1);
len2=strlen(s2);
for(i=0;i<len1;i++)
for(j=0;j<len2;j++)
{
t1=i;
t2=j;
sum=0;
time=k;
while(t1<len1&&t2<len2)
{
while(t1<len1&&t2<len2&&s1[t1]==s2[t2])
{
t1++;
t2++;
sum++;
}
if(time>0)
{
t1++;
t2++;
sum++;
time--;
}
else
break;
}
if(sum>max)
max=sum;
}
printf("%d",max);
return 0;
}
a [0], b [0] is the head, searching to find the maximum and maybe for the first string of first son, and a [0], b [1] ... then a [1], b [0] .. so, the maximum value of the sum is the maximum of all son string.