Ultraviolet A 531-compromise

Source: Internet
Author: User

The longest common sub-sequence, print path, pay attention to the format of printing.

The Code is as follows:

# Include <iostream> # include <cstdio> # include <cstdlib> # include <cstring> using namespace std; char w1 [100] [31], w2 [100] [31]; int dp [101] [101], path [101] [101]; void Print (int sum, int I, int j) // recursively print the path {if (! Sum) return; if (path [I] [j] = 0) {Print (sum-1, I-1, J-1); if (sum! = 1) printf ("% s", w1 [I-1]); else printf ("% s", w1 [I-1]); // No space after the last digit} else if (path [I] [j] = 1) Print (sum, I-1, j ); else if (path [I] [j] = 2) Print (sum, I, J-1);} int main () {# ifdef test freopen ("input.txt ", "r", stdin); # endif int cct1, cct2; while (scanf ("% s", w1 [cct1 = 0])! = EOF) {while (w1 [cct1] [0]! = '#') Scanf ("% s", w1 [++ cct1]); scanf ("% s", w2 [cct2 = 0]); while (w2 [cct2] [0]! = '#') Scanf ("% s", w2 [++ cct2]); for (int I = 0; I <100; ++ I) dp [0] [I] = dp [I] [0] = 0; for (int I = 1; I <= cct1; ++ I) for (int j = 1; j <= cct2; ++ j) {if (! Strcmp (w1 [I-1], w2 [J-1]) {dp [I] [j] = dp [I-1] [J-1] + 1; path [I] [j] = 0;} else {if (dp [I] [J-1] <dp [I-1] [j]) {dp [I] [j] = dp [I-1] [j]; path [I] [j] = 1 ;} else {dp [I] [j] = dp [I] [J-1]; path [I] [j] = 2 ;}}} print (dp [cct1] [cct2], cct1, cct2); puts ("");} 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.