Woj 1047 LCS problem (LCS algorithm summary)

Source: Internet
Author: User
Tags cas

http://acm.whu.edu.cn/land/problem/detail?problem_id=1047

Description

recently, Flymouse reads a book about algorithm and Data Structure. The book Reads:there is types of LCS problems. One is longest Common
subsequence problem. By the-the-programming, we could solve this problem. The other are longest Common Substring problem, which is
To find the longest string which is substrings of the strings. For example, given the following and the strings:
1. Flymouseenglishpoor
2. Comeonflymouseinenglish
the longest common substring is flymouse, the length of this string is 8.


Input

The first line contains a single integer t (1 <= t <=), the number of test cases. There would be lines and the Case,each line contains
a string (the length of the strings is no more than, and you can assure all strings won't contains any pun Ctuation or other separators).

Output

for each test case, you should output one line containing the longest common substring ' s length of the The test case.

Sample Input

1
Flymouseenglishpoor
Comeonflymouseinenglish


Sample Output

8

C + +
LCS algorithm:

The problem with the largest common substring of two strings is usually the following algorithm: Put the string 1 (length m) horizontal, String 2 (length n) vertical, get an MXN matrix C, the values of each element of the matrix are as follows, if M[I]=N[J], then c[j][i]=1, otherwise, c[j ][i]=0. Then find out that the longest diagonal line in the matrix is 1, and the length of the diagonal is the length of the common substring.

The following is the matching matrix for string 21232523311324 and string 312123223445, which is in the x direction and the latter is in the Y direction. Not hard to find, the red part is the longest matching substring. By finding the location we get the longest matching substring: 21232

0 0 0 1 0 0 0 1 1 0 0 1 0 0 0
0 1 0 0 0 0 0 0 0 1 1 0 0 0 0
1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 0 1 1 0 0 0 0
1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 1 1 0 0 1 0 0 0
1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 1 1 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

but finding the longest 1 diagonal sequence in a matrix of 0 and 1 also takes a certain amount of time. This part of the time can be omitted by improving the way the matrix is generated and by setting tag variables. Here's how the new matrix is generated:

0 0 0 1 0 0 0 1 1 0 0 1 0 0 0
0 1 0 0 0 0 0 0 0 2 1 0 0 0 0
1 0 2 0 1 0 1 0 0 0 0 0 1 0 0
0 2 0 0 0 0 0 0 0 1 1 0 0 0 0
1 0 3 0 1 0 1 0 0 0 0 0 1 0 0
0 0 0 4 0 0 0 2 1 0 0 1 0 0 0
1 0 1 0 5 0 1 0 0 0 0 0 2 0 0
1 0 1 0 1 0 1 0 0 0 0 0 1 0 0
0 0 0 2 0 0 0 2 1 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Needless to say, you might have seen it. When a character is matched, we do not simply assign 1 to the corresponding element, but instead assign the value of the upper-left element to add one. We use two tag variables to mark the position of the largest element in the matrix, in the process of matrix generation to determine whether the value of the currently generated element is the largest, thereby changing the value of the tag variable, then to the completion of the matrix, the longest matching substring of the position and length has been out.

it's faster, but it takes too much space. We have noticed that in the improved matrix generation, the front row is useless for each generated row. So we just use one-dimensional arrays.

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 1111# Define RST (n) memset (n, 0, sizeof (n)) using namespace Std;char S1[MAXN], s2[maxn];int LCS (char *s1, char *s2) {int L1 = s    Trlen (S1), L2 = strlen (s2);    int* C = new INT[L2];    int begin, End = 0, len = 0;  for (int i=0, i<l1; i++) {for (int j=l2-1; j>=0; j--) {if (s1[i] = = S2[j]) {if (i==0                && j==0) C[j] = 1;            else c[j] = c[j-1] + 1;            }else C[j] = 0;                if (C[j] > len) {len = c[j];            end = J;    }}} return len;    /* char* pos = new char[len+1];    Begin = End-len + 1;    for (int i=begin; i<end; i++) {Pos[i-begin] = S2[i];    } Pos[len] = ' + ';    return POS;    */}int Main () {int cas;    scanf ("%d", &cas);    GetChar ();        while (cas--) {scanf ("%s%s", S1, S2);    printf ("%d\n", LCS (S1, S2)); } RETUrn 0;} 


Woj 1047 LCS problem (LCS algorithm summary)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.