String Master [greedy]

 

 

I is limited, at the problem is not the solution, a lot of understanding

 

Thank you for watching this konjac

 

 

topic:

String Master(master.c/cpp/pas)
Input fifile: master.in
Postings fifile: master.out
Time limit: 1 seconds
Memory limit: 128 megabytes
The so-called longest common string, such as string A: "abcde", string B: "jcdkl", then their longest common string is the string "cd", i.e., long
Of the longest string, and have appeared as a continuous string in two sub-strings.
Given two strings of length n are, you, the string for the master, they find the longest common substring be easier.
So now you have the opportunity to modify k times, each time you can choose a location of a string, edit it into any character.
You need to modify the rational use of this opportunity k times, after making two strings to modify the longest common substring longest. I believe that the master string
For you, this problem is not beat you.
Input
The first line contains two integers n, k, which represent the length and number of modified character strings.
The second line contains a string consisting only of lowercase characters n S.
The third line contains a string consisting only of lowercase characters n T.
Output
Output line an integer, i.e. two strings after the modification is completed longest common substring length.

Sample Input1

5 0
abcde
jcdkl

Sample Output1

 

Sample Input2

5 2

Sample Output2

aAAAA
Ababa
 
 
This question has a greedy thoughts.
 
Greedy strategy is as follows:
 
我们用一个limit表示当上下两个字符串不相等时能加到的最大上限值,当limit大于k时,我们就回溯break,我们能保
 
证为最优的。开三个for循环,分别枚举字符串a,字符串b,以及当前能到达的最大个数。
 
这时我们要不断sum++。因为有k后悔的存在,保证我们求出相等段数的最大值。最后在用ans来存最大值即可。
 
code:
 1 #include<bits/stdc++.h>
 2 #pragma GCC optimize(3)
 3 using namespace std;
 4 int n,k,ans;
 5 char s[1001];
 6 char ch[1001];
 7 void inint(){
 8     freopen("master.in","r",stdin);
 9     freopen("master.out","w",stdout);
10 }
11 inline int read(){
12     int x=0,f=1;char ch=getchar();
13     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
14     while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
15     return x*f;
16 }
17 inline void write(int x)
18 {
19     if(x<0)x=-x,putchar('-');
20     if(x>9)write(x/10);
21     putchar(x%10+'0');
22 }
23 int main()
24 {
25     //inint();
26     n=read(),k=read();
27     scanf("%s",s+1);
28     scanf("%s",ch+1);
29     for(int i=1;i<=n;i++){
30         for(int j=1;j<=n;j++){
31             int limit=0,sum=0;
32             for(int kk=0;i+kk<=n&&j+kk<=n;kk++){//不能超过上限的n 
33                 if(s[i+kk]!=ch[j+kk]){
34                     limit++;//使用k 
35                 }
36                 if(limit>k){//使用次数超过k时,回溯 
37                     break;
38                 }
39                 //cout<<"k= "<<k<<" i= "<<i<<" j= "<<j<<" limit= "<<limit<<endl;
40                 
41                 sum++;//累加相同的,如果k=0的话,如果不相等时,会不断回溯。 
42             }
43             ans=max(ans,sum);//存最大值 
44         }
45     }
46     printf("%d\n",ans);
47     return 0;
48 }
49 /*
50 
51 5 0 
52 abcde 
53 jcdkl
54 2
55 5 2
56 aaaaa
57 ababa
58 5
59 10 3
60 abcdefghij
61 kkkyzlabcd
62 
63 */

 

 

Guess you like

Origin www.cnblogs.com/nlyzl/p/11818271.html