[程式設計與演算法(2)]第五周第二題結題報告: Zipper

來源:互聯網
上載者:User

標籤:imp   data   original   字元   contain   sed   printf   通過   each   

題目連結:http://cxsjsxmooc.openjudge.cn/2017t2springhw5/2/

----------------------------------------------題目------------------------------------------------------

描述

Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in its original order.

For example, consider forming "tcraete" from "cat" and "tree":

String A: cat
String B: tree
String C: tcraete

As you can see, we can form the third string by alternating characters from the two strings. As a second example, consider forming "catrtee" from "cat" and "tree":

String A: cat
String B: tree
String C: catrtee

Finally, notice that it is impossible to form "cttaree" from "cat" and "tree".
輸入The first line of input contains a single positive integer from 1 through 1000. It represents the number of data sets to follow. The processing for each data set is identical. The data sets appear on the following lines, one data set per line.

For each data set, the line of input consists of three strings, separated by a single space. All strings are composed of upper and lower case letters only. The length of the third string is always the sum of the lengths of the first two strings. The first two strings will have lengths between 1 and 200 characters, inclusive.
輸出For each data set, print:

Data set n: yes

if the third string can be formed from the first two, or

Data set n: no

if it cannot. Of course n should be replaced by the data set number. See the sample output below for an example.
範例輸入

3cat tree tcraetecat tree catrteecat tree cttaree

範例輸出

Data set 1: yesData set 2: yesData set 3: no

-------------------------------------------------題解------------------------------------------------------

本題是一道DP的題目,核心問題就是“狀態”。

我假設有i, j, k三個游標分別指向s1[i], s2[j], s3[k]的位置。

那麼對於cat[0] tree[0] tcraete[0], 對於s3的t字元,必須從s2[0]位置拿走,本問題轉化為: cat[0] tree[1] tcraete[1]

依次進行,直到

cat\0[3] tree\0[4] tcraete\0[7]

 

對於範例3,cat tree cttaree字串進行上述類比,在

cat[1] tree[1] cttaree[2]時,發現s3字串的當前字元無法從s1,s2中對應游標進行匹配。

 

由此可以看出,當k游標可以走到s3字元的末尾,則代表s3字元可以由s1 s2組成。

 

對於範例2,存在一種情況:

當游標讀到cat[2] tree[0] catrtee[2]時,即可以讀s1的‘t‘,也可以讀s2的‘t‘,存在岔路。

在這種情況下,我們需要對兩條路都進行試探,而兩條路得到的解(true/false)中只要有一條能通,本範例就應該輸出Yes,因此是“或”關係。

 

此外,i, j, k中,k=i+j,因此本題的dp狀態為二維。即solve(i,j)

 

在代碼實現中,通過建立dp數組來記錄已經計算過的路徑,保證每個狀態(i, j)最多被訪問一次。(當s1 s2中出現大量岔路的情況,不做狀態記錄處理會產生大量的重複遞迴調用,嚴重逾時)。

 

實現代碼如下: 

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4  5 #define DEBUG 0 6  7 using namespace std; 8  9 char s1[500], s2[500], s3[500];10 int s3_len;11 int dp[500][500];12 13 void reset_dp() {14     for (int i = 0; i < 500; ++i)15         for (int j = 0; j < 500; ++j)16             dp[i][j] = -1;17 }18 19 bool solve(int i, int j, int k) {20     if (dp[i][j] != -1) {21         return dp[i][j];22     }23     if (k == s3_len) {24         dp[i][j] = 1;25         return dp[i][j];26     }27     if (s1[i] != s3[k] && s2[j] != s3[k]) {28         dp[i][j] = 0;29         return dp[i][j];30     }31     if (s1[i] == s2[j]) {32         dp[i][j] = solve(i + 1, j, k + 1) || solve(i, j + 1, k + 1);33         return dp[i][j];34     }35     else if (s1[i] == s3[k])36         return solve(i + 1, j, k + 1);37     else38         return solve(i, j + 1, k + 1);39 }40 41 int main() {42     int k;43     cin >> k;44     for (int i = 1; i <= k; ++i) {45         cin >> s1 >> s2 >> s3;46         s3_len = strlen(s3);47         reset_dp();48         if (solve(0, 0, 0)) {49             printf("Data set %d: yes\n", i);50         }51         else {52             printf("Data set %d: no\n", i);53         }54     }55     return 0;56 }

 通過:

 

[程式設計與演算法(2)]第五周第二題結題報告: Zipper

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.