Algorithm: HDU 4681 String (DP, LCS | multi-school 8)

Source: Internet
Author: User
Tags cas strlen

Meaning

Give 3 string a,b,c, you need to find a string d, to meet the following rules

A) d is a subsequence of a

b) d is a sub sequence of B

c) c is a substring of D

To find the maximum length of D

To note the difference between subsequence and substring, the subsequence is discontinuous and the string is continuous

Ideas

By the title, C must be a subsequence of a and B, let's assume that C has only one subsequence in A and B, look at the following example:

Abcdefdeg

Acebdfgh

Cf

You can see "CF" In the [3, 6] interval of a string, in the [2,6] interval of the B string (yellow background)

Because the desired c is a substring of D, the other letters in the yellow range must not be taken.

So this longest length is equal to the length of the longest common subsequence length +c of the red area and the blue region

The F (i, j) of the LCS algorithm asks for the longest common string of the first and last J of the second string.

Then the red part can be obtained directly.

In the back section, you just need to turn the two strings upside down and ask for one at the LCS.

Finally, find all C in the a,b sequence range, enumerate to get the answer

Code

/**========================================== * This are a solution for ACM/ICPC problem * * @source: HDU-4681 String * @au Thor:shuangde * @blog: blog.csdn.net/shuangde800 * @email: zengshuangde@gmail.com *================================== =========*/#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #inc
    
Lude <queue> #include <cmath> #include <cstring> #define MP make_pair using namespace std;
typedef long long Int64;
typedef pair<int,int > PII;
const int INF = 0X3F3F3F3F;
    
Const double PI = ACOs (-1.0);
    
const int MAXN = 1010;
    
Vector<pii >VT1, VT2;
Char A[MAXN], A2[MAXN];
Char B[MAXN], B2[MAXN];
    
Char C[MAXN];
int F1[MAXN][MAXN];
    
int F2[MAXN][MAXN];
int len1, len2, Len3;
    
    
int A1, A2, B1, B2; void reverse () {for (int i = 1; I <= len1, ++i) a2[i] = A[len1+1-i]; for (int i = 1; I <= len2; ++i) b2[i] = b[len2+1-
I]; void LCS (char* str1, char* str2, int F[MAXN][MAXN])
{memset (f, 0, sizeof (f)); for (int i = 1; I <= len1; ++i) {for (int j = 1; j <= Len2; ++j) {if (str1[i] = = Str2[j])
F[I][J] = f[i-1][j-1] + 1;
else f[i][j] = max (F[i-1][j], f[i][j-1]);  }}//Get string interval//http://www.bianceng.cn void Getcs (char* str, int len, vector<pii>& vt) {for (int i = 1; i <= Len; ++i) if (str[i]==c[1]) {int p = i, j = 1; while (J <= len3 && p <= len) {if (c[j] = = Str[p]) {++p; ++j;} els
e{++p}}
    
if (j = = Len3 + 1) {Vt.push_back (MP (i, p-1));} 
    
int main () {int ncase; int cas = 1; scanf ("%d", &ncase); while (ncase--) {scanf ("%s%s%s", A+1, B+1, c+1);
Len1 = strlen (a+1);
Len2 = strlen (b+1);
    
Len3 = strlen (c+1);
LCS (A, B, F1);
Reverse ();
    
LCS (A2, B2, F2);
Vt1.clear ();
    
Vt2.clear ();
Getcs (A, Len1, VT1);
    
Getcs (B, Len2, VT2);
int ans = 0; for (int i = 0; i < vt1.size (); ++i) {for (int j = 0; J < vt2.size (); ++j) {int a1 = Vt1[i].first, a2 = Vt1[i].secon
D int B1 = Vt2[j].first,b2 = Vt2[j].second;
int tmp = F1[a1-1][b1-1] + f2[len1-a2][len2-b2] + Len3;
ans = max (ans, TMP);
} printf ("Case #%d:%d\n", cas++, ans);
return 0; }

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.