Blue Bridge cup 2015- campus selection -C / C ++ - A title group 1

Foreword

Winter vacation in the Blue Bridge Cup race preparation, did some topics, but also look correct me if wrong ......

topic

Substring of a string of consecutive local means of the string. If you do not require continuous, it can be called a sub-sequence.
For example, the string: "abcdefg" purposes, "ab", "abd" , "bdef" and the like are its sequence.
In particular, a string itself, and also its empty string sequences.

For two strings, it can have many common subsequence, we are concerned with: the maximum length of the sequences they have in common is how long. The following code solving this problem. Please fill in the code underlined part missing.

Note: Fill in only the underlined part of the lack of content, do not fill in any extra symbols or comments, descriptions. For example, do not fill in parentheses has been given.

inline max(int a, int b)
{
     return a>b?a:b;
}

int f(char* x, char* y)
{
     if(strlen(x)==0) return 0;
     if(strlen(y)==0) return 0;
    
     if(*x == *y) return f(x+1, y+1) + 1;
    
     return max(__________);
}

int main()
{
     printf("%d\n", f("ac","abcd")); //2
     printf("%d\n", f("acebbcde1133","xya33bc11de")); //5
     return 0;
}

answer

lcs problem, you should fill out the f (x, y-1), f (x-1, y)

Code

 1 #include<iostream>
 2 #include<string.h>
 3 #include<stdio.h>
 4 using namespace std;
 5 int max(int a, int b)
 6 {
 7      return a>b?a:b;
 8 }
 9 
10 int f(char* x, char* y)
11 {
12      if(strlen(x)==0) return 0;
13      if(strlen(y)==0) return 0;
14      
15      if(*x == *y) return f(x+1, y+1) + 1;
16      
17      return max( f(x,y+1),f(x+1,y));
18 }
19 
20 int main(){
21      char a[]="acebbcde1133";
22      char b[]="xya33bc11de";
23      //printf("%d\n", f(a,b)); //2
24      printf("%d\n", f(a,b)); //5
25      return 0;
26 } 

 

 

Guess you like

Origin www.cnblogs.com/memocean/p/12197959.html