Ultraviolet A 10066-the Twin Towers

Source: Internet
Author: User

The longest common subsequence is required.

Question type: DP/LCs

Question Analysis:

Didn't I wake up? Why is it clearly written: // print a blank line after the output of each data set, instead of a blank line between each group of data. It turned out to be a long time.

 Code:

① The version of the memory-based search.

// In fact, the topic is about the longest common subsequence. Fuck. <Br/> # include <cstdio> <br/> # include <cstring> <br/> using namespace STD; <br/> int N1, N2; <br/> # define maxn 102 <br/> int d [maxn] [maxn]; <br/> int vis [maxn] [maxn]; <br/> int A [maxn], B [maxn]; <br/> int dp (int I, Int J) <br/>{< br/> if (I <0 | j <0) <br/>{< br/> return 0; <br/>}< br/> If (vis [I] [J]) return d [I] [J]; <br/> vis [I] [J] = 1; <br/> int Max; <br/> if (a [I] = B [J]) <br/> max = dp (I-1, J-1) + 1; <br/> else <Br/> {<br/> max = dp (I, J-1)> dp (I-1, j )? DP (I, J-1): dp (I-1, J); <br/>}< br/> return d [I] [J] = max; <br/>}< br/> inline int RINT () {int X; scanf ("% d", & X); Return X ;} <br/> int main () <br/> {<br/> int T = 1; <br/> while (1) // when at least one is not 0 <br/>{< br/> n1 = RINT (); N2 = RINT (); <br/> If (! N1 &&! N2) break; <br/> // If (T! = 1) printf ("/N"); <br/> memset (VIS, 0, sizeof (VIS); <br/> for (INT I = 0; I <N1; I ++) <br/> A [I] = RINT (); <br/> for (INT I = 0; I <N2; I ++) <br/> B [I] = RINT (); <br/> int ans = dp (n1-1, n2-1 ); <br/> printf ("Twin Towers # % d/N", t ++); <br/> printf ("Number of tiles: % d/N ", ans); <br/> printf ("/N"); // print a blank line after the output of each data set. <br/>}< br/>}

From zcoder, http://www.zhaocong89.com /? P = 498

Returns the version of the state transition equation.

# Include <iostream> <br/> # include <fstream> <br/> # include <cstring> <br/> # include <cstdlib> <br/> using namespace STD; <br/> # ifndef online_judge <br/> ifstream fin ("test. in "); <br/> ofstream fout (" test. out "); <br/> # else <br/> # define fin CIN <br/> # define fout cout <br/> # endif <br/> const int maxn = 105; <br/> int n, m; <br/> int T1 [maxn]; <br/> int T2 [maxn]; <br/> int d [maxn] [maxn]; <br/> int main () <br/> {<br/> I NT ncase = 0; <br/> while (Fin> N> M &&(! (N = 0 & M = 0) <br/>{< br/> // If (ncase) <br/> // fout <Endl; <br/> memset (D, 0, sizeof (d); <br/> for (INT I = 1; I <= N; I ++) <br/> fin> T1 [I]; <br/> for (INT I = 1; I <= m; I ++) <br/> fin> T2 [I]; <br/> for (INT I = 1; I <= N; I ++) <br/> {<br/> for (Int J = 1; j <= m; j ++) <br/> {<br/> If (T1 [I] = t2 [J]) <br/> d [I] [J] = d [I-1] [J-1] + 1; <br/> else <br/> d [I] [J] = max (d [I-1] [J], d [I] [J-1]); <br/>}< br/> fout <"Twin Towers #" <(++ ncase) <Endl; <br/> fout <"Number of tiles:" <D [N] [m] <Endl; <br/>}< br/> return 0; <br/>}

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.