uva 1509 - Leet(暴力)

來源:互聯網
上載者:User

標籤:style   http   color   os   io   for   

題目連結:uva 1509 - Leet

題目大意:給出k,表示一個字元可以對應k給字元編碼,給出字串1,問時候有符合的編碼可以產生字串2.

解題思路:暴力枚舉,對於每個碰到的字元記錄對應的編碼。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 105;const int maxc = 200;int k, len1, len2, rec[maxc][3];char str1[maxn], str2[maxn];bool check (int l, int r, int pos) {    for (int i = 0; i + l <= r; i++) {        if (str2[l+i] != str2[pos+i])            return false;    }    return true;}bool dfs (int pos1, int pos2) {    if (pos1 == len1)        return pos2 == len2;    if (pos2 == len2)        return false;    char ch = str1[pos1];    if (rec[ch][0] == -1) {        rec[ch][0] = pos2;        for (int i = 1; i <= k; i++) {            rec[ch][1] = pos2+i-1;            if (dfs(pos1+1, pos2 + i))                return true;        }        rec[ch][0] = -1;    } else {        if (!check(rec[ch][0], rec[ch][1], pos2))            return false;        if (dfs(pos1+1, pos2 + rec[ch][1] - rec[ch][0] + 1))            return true;    }    return false;}int main () {    int cas;    scanf("%d", &cas);    while (cas--) {        scanf("%d%s%s", &k, str1, str2);        memset(rec, -1, sizeof(rec));        len1 = strlen(str1);        len2 = strlen(str2);        printf("%d\n", dfs(0,0) ? 1 : 0);    }    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.