[CSP-S Simulation Test]: String Master (violence)

Title Description

The so-called longest common substring, such as the string $ A: "abcde" $, string $ B: "jcdkl" $, then their longest common substring string $ "cd" $, i.e. the length of the longest string, and the two strings are appeared as a continuous substring.
Given two are RnR length of the string, the string for the master you, seeking their longest common string could not be easier.
So now you have the opportunity to modify the $ k $ times, each time you can choose a location of a string, edit it into any character.
After this you need to use reasonable $ k $ revision chance that modify two strings longest common string is the longest. I believe that you are the master of strings, this problem also beat you.


Input Format

The first line contains two integers $ n, k $, which represent the length and number of modified character strings.
The second line comprises a length of n-$ $ $ S $ string consisting of lower case characters only.
The third line comprises a length of n-$ $ $ T $ string consisting of lower case characters only.


Output Format

Output line an integer, i.e. two strings after the modification is completed longest common substring length.


Sample

Sample input 1:

5 0
abcde
jcdkl

Sample output 1:

2

Sample input 2:

5 2
aaaaa
Ababa

Sample Output 2:

5


Data range and tips

For $ 100 \% $ data, $ 0 \ leqslant k \ leqslant n $.


answer

Rare water problem, but I went out to play, so did not test ......

Mad come back to see the title, a bit difficult, not ......

Then I read the data range, which is not the question ** Well ......

So $ 5 $ minutes cut.

And I know a lot of people are playing $ DP $, in fact, violence can be.

We must be attached to a good time to use a few modifications that do not match the opportunity, so we can enumerate the starting point of two strings, and then comparing bit by bit, it is not the same opportunity to put spent, know no modification or coming to an end so far.

A story, $ skyh $ see this question called $ DP $, and then want to make a film about violence against, kick found the time complexity is the same, it is very embarrassing, finished the story ......

So you do not want to see the simple question complicated.

Time complexity: $ \ Theta (n ^ 3) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
int n,k;
char S[301],T[301];
int l,r,sum;
int ans;
int main()
{
	scanf("%d%d%s%s",&n,&k,S+1,T+1);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			l=i;
			r=j;
			sum=0;
			while(l&&r&&sum<=k)
			{
				if(S[l]!=T[r])sum++;
				l--;
				r--;
			}
			if(sum>k)ans=max(ans,i-l-1);
			else ans=max(ans,i-l);
		}
	printf("%d",ans);
	return 0;
}

rp++

Guess you like

Origin www.cnblogs.com/wzc521/p/11574313.html
Recommended