Blue Bridge Cup: up to the same sequence (LCS) memory problems Search Algorithm

Blue Bridge Cup: up to the same sequence (LCS) memory problems Search Algorithm

375ms compared to the recursive method, optimized for speed is very fast
Here Insert Picture Description

Problem description
  provided x (i), y (i ), z (i) represents a single character, then X = {x (1) x (2) ...... x (m)}, Y = {y (1) y ( 2) ...... y (n)} , Z = {z (1) z (2) ...... z (k)}, we call a sequence of characters, wherein m, n and k are a sequence of characters X, Y, Z length, parentheses () is referred to in the subscript character sequence.
  If a length greater than 0 and strictly increasing subscript sequence is present {i1, i2 ...... ik}, such that for all j = 1,2, ...... k, have x (ij) = z (j ), then we call Z is the character sequence of X. Moreover, if the Z character sequences of both X and Y are sequences of characters, then we call X and Z is the public character sequence Y.
  In today's issue, we want to calculate the maximum length of the character sequence common character sequences X and Y of two given, here we only require the length of the output value of the maximum length common subsequence corresponding.
  For example, the character sequence X = abcd, Y = acde, then the maximum length thereof is 3, the corresponding common character sequences acd.
Input format
  input line, two character strings separated by spaces
output format of
  the output values of both the maximum length of length of the common character sequence corresponding to the sequence of characters
Sample Input

AABB AABB

Sample Output

2

Data size and Conventions
  input string length of up to 100, case sensitive.

Thinking

If you look at recursive tree, we can see that there are a lot of duplicate branches are double counting , which greatly waste of time, so we will save calculated results together, each first check before the recursive whether the result will be obtained before being calculated can greatly save time

  • As a result of map is made up of two key queries, so the use of two-dimensional array
int result[maxlen][maxlen];	// 存放结果 

Conventions:

Len1 length s1 before the sub-string is a string sub1, sub-strings before len2 string length s2 is sub2

Substring s1 [string length before len1] and [s2 sub-string length before len2] longest string length of the same sequence sub_len

Substring s1 [string length before len1-1] and [s2 sub-string length before len2] longest string length of the same sequence sub_len1

Substring s1 [string length before len1] and [s2 sub-string length before len2-1] longest string length of the same sequence sub_len2

  • So if the last sub1 and sub2 if the same, then they are the same as the length of the longest sequence is sub_len + 1
  • If sub1 and sub2 last bit different, then they are the same as the length of the longest sequence is the max (sub_len1, sub_len2)

AC complete code

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

#define max(a, b) ((a>b) ? (a) : (b))
#define maxlen 114
string s1, s2;
int result[maxlen][maxlen];	// 存放结果 

/*
param len1 : s1串的前len1长度的子串 sub1
param len2 : s2串的前len2长度的子串 sub2
return 	   : sub1 和 sub2 最大相同序列的长度 
*/
int dp(int len1, int len2)
{
	if(len1>=1 && len2>=1)
	{		
		if(s1[len1-1] == s2[len2-1])
		{
			int l;
			if(result[len1-1][len2-1])
			{
				return result[len1-1][len2-1] + 1;
			}
			else
			{
				l = dp(len1-1, len2-1);
				result[len1-1][len2-1] = l;
			}
			return l + 1;
		}
		else
		{
			int l1;
			if(result[len1-1][len2])
			{
				l1 = result[len1-1][len2];
			}
			else
			{
				l1 = dp(len1-1, len2);
				result[len1-1][len2] = l1;
			}
			
			int l2;
			if(result[len1][len2-1])
			{
				l2 = result[len1][len2-1];
			}
			else
			{
				l2 = dp(len1, len2-1);
				result[len1][len2-1] = l2;
			}
			
			return max(l1, l2);
		}
	}
	else
	{
		return 1;
	}
}

int main()
{
	 memset(result, 0, sizeof(result));	// 初始全为0
	 
	 cin>>s1>>s2;
	 
	 cout<<(dp(s1.length(), s2.length())-1)<<endl;
	
	return 0;
}


Published 19 original articles · won praise 0 · Views 120

Guess you like

Origin blog.csdn.net/weixin_44176696/article/details/103996658
Recommended