spoj33&poj1934 Trip DP, enumeration

Source: Internet
Author: User

Topic Portal: https://www.luogu.org/problemnew/show/SP33

Title: Give two strings, the length of its LCS (the longest common subsequence) and the specific scheme (the same string counts as the same scheme). Number of data groups $\leq 10$, string length $\leq 80$, scheme number $\leq 1000$

Originally thought this is an LCS water problem, the result is super low of various output solution method tle to suspect life

So a tall on the output method appears (for reference to 41170117)

Open more than two auxiliary arrays $f_{i,j}$ and $g_{i,j}$ represent $s1$ and $s2$ from $1$ characters to $i$ characters $j$ ($a $ corresponds to $0$, $b $ corresponds to $1$, and so on) last occurrence, after calculating the two arrays, Recursive output, using the enumeration method, from the back to the next one to enumerate the current position can be filled with letters.
Set $dfs (A, B, c) $ indicates the current $s1$ length is $a$, the length of the $s 2$ is $b$, the number of letters to enumerate is $c$ when the enumeration process, enumeration $0-25$, get that letter $f_{a,b}$ and $g_{a,b}$, if $s1$ length is $f_{a , b}$, $s 2$ the longest common subsequence length of $g_{a,b}$ is exactly $c$, then $dfs (F_{a,b}-1, g_{a,b}-1, c-1) $, a string that satisfies test instructions when the first bit is enumerated

Why can you do a lot of pruning in this way? Example:

$ABCABCAA $
$ACBACBA $

Above is the sample data, the longest common subsequence length is $5$. Consider $dfs (7,6,5) $ when enumerating to $a$ (string starting from $0$), at this point $f = 7, G = 6$, $dfs (6,5,4) $. If you do not consider the enumeration above, our options are $ (0,0) (0,3) (0,6) (3,0) (3,3) (3,6) (6,0) (6,3) (6,6) (7,0) (7,3) (7,6) $ altogether $12$ species, since the same string counts as the same scheme, by which greed can know $ ( 7,6) $ is the current best choice and can contain all the other conditions, so you only need to continue searching to $ (7,6) $.

#include <bits/stdc++.h>using namespacestd;stringS1, S2;vector<string>s; Shortmaxn[Bayi][Bayi], last1[ -][Bayi], last2[ -][Bayi], cou;//Last1, last2 corresponds to the above F, G. InlineintMaxintAintb) {    returna > B?a:b;}voidCreateintA1,intA2,intNumstringSS) {    if(num = =0) {s.push_back (ss); return; }     for(inti =0; I < -; i++)        if(LAST1[I][A1] >= num && last2[i][a2] >= num && maxn[last1[i][a1]][last2[i][a2]] = =num) Create (LAST1[I][A1]-1, LAST2[I][A2]-1, Num-1, (Char)('a'+ i) +SS); //recursive output is the most important process!!! }intMain () {Ios::sync_with_stdio (0); intT;  for(Cin >> T; T t--) {memset (MAXN,0,sizeof(MAXN)); memset (Last1,0,sizeof(Last1)); memset (Last2,0,sizeof(LAST2)); CIN>> S1 >>S2;  for(inti =1; I <= s1.size (); i++)             for(intj =1; J <= S2.size (); J + +){                if(S1[i-1] = = S2[j-1]) Maxn[i][j] = max (Maxn[i][j], maxn[i-1][j-1] +1); MAXN[I][J]= Max (Maxn[i][j], max (Maxn[i-1][J], Maxn[i][j-1])); }            //LCS DP Solver         for(inti =1; I <= s1.size (); i++)             for(intj =0; J < -; J + +)                if(S1[i-1] -'a'= = j) Last1[j][i] =i; ElseLast1[j][i] = last1[j][i-1];  for(inti =1; I <= s2.size (); i++)             for(intj =0; J < -; J + +)                if(S2[i-1] -'a'= = j) Last2[j][i] =i; ElseLast2[j][i] = last2[j][i-1]; Create (S1.size (), S2.size (), Maxn[s1.size ()][s2.size ()],"");        Sort (S.begin (), S.end ());  for(inti =0; I < s.size (); i++) cout << S[i] <<Endl; //Outputs.clear (); cout<<Endl; Cou=0; }    return 0;}

spoj33&poj1934 Trip DP, enumeration

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.