POJ 2192-zipper(動態規劃)

來源:互聯網
上載者:User

    題意為:輸入三個字串 str1, str2, str3;   要判斷 str3 能否 由 str1 和 str2 組成。

                例如:cat 和 tree 能組成 tcraete 所以輸出yes.

    費了我老半天時間,想破了腦袋,想了n種實現方法,最後發現一種自認為很好的方法。然後馬上寫代碼 提交,直接就來了個逾時,

  把我都搞茫然了,這題資料範圍本來就很小 ,而且自認為自己的方法 應該是很省時間的,沒有重複計算浪費時間。。.....

        最後 看了 discuss 終於明白了 有一種情況

        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaab
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaac
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaacaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab
        我的 程式運行這個資料 要用好幾秒鐘的時間。

  還是要看人家的代碼,還是老方法。

 

#include <stdio.h>#include <string.h>int main(){    int  i, j, k, t;    int  L1, L2, ok[202][202];    char str1[201], str2[201], str3[402];                    /**********************                      ok[i][j]為真時表示str1的前i個字元可以和str2的前j個字元                          組成str3的前i+j個字元。為假時 表示不能。                    ***********************/    scanf("%d", &t);    for (i=1; i<=t; i++)    {        scanf("%s %s %s", str1, str2, str3);        memset(ok, 0, sizeof(ok));        L1 = strlen(str1);        L2 = strlen(str2);        ok[0][0] = 1;        for (j=1; j<=L1; j++)        {            if (ok[j-1][0] == 1 && str1[j-1] == str3[j-1])                ok[j][0] = 1;            else                break;        }        for (j=1; j<=L2; j++)        {            if (ok[0][j-1] == 1 && str2[j-1] == str3[j-1])                ok[0][j] = 1;            else                break;        }        for (j=1; j<=L1; j++)        {            for (k=1; k<=L2; k++)            {                if (ok[j-1][k]==1 && str1[j-1]==str3[j+k-1] || ok[j][k-1]==1 && str2[k-1]==str3[j+k-1])                    ok[j][k] = 1;            }        }        if (ok[L1][L2] == 1)            printf("Data set %d: yes/n", i);        else            printf("Data set %d: no/n", i);    }}

 

   但是 又 發現 yuyu_myself 居然是用的 DFS 做的, 而且時間,記憶體,代碼長度都比 我的斷的多。

  這就奇怪了, 動態規劃 不是 最節約時間的嗎?  難道是 這題的 資料太小了的原因, 顯現不出來DP 的優勢?

 

#include <stdio.h>#include <string.h>#define N 202char s1[N], s2[N], s[N+N];int dfs(int x, int y){    if (x == -1 && y == -1)        return 1;    if (x >= 0  && s1[x] == s[x + y + 1])        if (dfs(x - 1, y))            return 1;    if (y >= 0 && s2[y] == s[x + y + 1])        if (dfs(x, y - 1))            return 1;    return 0;}int main(void){    int n, i;    scanf("%d", &n);    for (i = 1; i <= n; i++)    {        scanf("%s %s %s", s1, s2, s);        if ( dfs(strlen(s1)-1, strlen(s2)-1) )            printf("Data set %d: yes/n", i);        else            printf("Data set %d: no/n", i);    }    return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.