UVaLive 5844 (UVa1509) Leet

题目

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=513&page=show_problem&problem=3855

题意

一个长度小于15的小写字母组成的字符串中,每个字符可替换成1-k个不同的字符且只有一种替换,给出原串和替换后的串以及k,判断是否合法

解法

数据很小,裸DFS

代码

#include <cstdio>
#include <cstring>
char s1[20], s2[50];
int match[30], cnt[30];
int len1, len2, k;
int dfs(int x1, int x2) {
    if(x1 == len1 && x2 == len2)
        return 1;
    int id = s1[x1] - 'a';
    int sum = 0;
    for(int kk = 0; kk < k && x2 + kk < len2; kk++) {
        sum = sum * 255 + s2[x2 + kk] ;
        if(match[id] == -1 || match[id] == sum) {
            match[id] = sum;
            cnt[id] ++;
            if(dfs(x1 + 1, x2 + kk + 1))
                return 1;
            cnt[id] --;
            if(!cnt[id])
                match[id] = -1;
        }
    }
    return 0;
}
int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &k);
        scanf("%s %s", s1, s2);
        len1 = strlen(s1);
        len2 = strlen(s2);
        memset(match, -1, sizeof(match));
        memset(cnt, 0, sizeof(cnt));
        printf("%d\n", dfs(0, 0));
    }
    return 0;
}

Source

2011 Asia Daejeon Regional Contest

posted @ 2015-08-16 12:10  ACM_Record  阅读(95)  评论(0编辑  收藏  举报