uva 1509 Leet

点击打开链接uva 1509

思路:dfs+回溯
分析:
1 给定两个字符串和值k,判断字符串1是否可以映射成字符串2.
2 题目说了一个字符最多可以映射为k个字符,那么我们就不能直接去枚举判断了,所以我们采用搜索的方法,因为每一个字符映射的范围是确定的,那么我们通过搜索和回溯判断即可

代码:

#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 100;
char str[maxn] , mapStr[maxn];
map<char , char*>mp;
int k , len1 , len2;

int dfs(int pos1 , int pos2){
    if(pos1 == len1 && pos2 == len2)//最终成立的条件
       return 1;
    if(pos1 == len1 && pos2 != len2)//当pos1 = len1的时候不能在递归了
       return 0;
    if(mp[str[pos1]]){//如果已经有值
       char *tmp = mp[str[pos1]];
       int len = strlen(tmp);
       int tmpPos = pos2;
       for(int i = 0 ; i < len ; i++){
          if(tmp[i] != mapStr[tmpPos])
             return 0;
          tmpPos++;
       }
       if(dfs(pos1+1 , tmpPos))
          return 1;
    }
    else{//还没有值
       char tmp[maxn];
       for(int i = 1 ; i <= k ; i++){
          memset(tmp , '\0' , sizeof(tmp));
          int pos = 0;
          for(int j = pos2 ; j < pos2+i ; j++)
             tmp[pos++] = mapStr[j];
          mp[str[pos1]] = tmp;
          if(dfs(pos1+1 , pos2+i))
             return 1;
          mp[str[pos1]] = 0;//回溯
       }
    }
    return 0;
}

int main(){
    int Case;
    scanf("%d" , &Case);
    while(Case--){
        scanf("%d%*c" , &k);
        gets(str);
        gets(mapStr);
        len1 = strlen(str);
        len2 = strlen(mapStr);
        mp.clear();
        printf("%d\n" , dfs(0 , 0));
    }
    return 0;
}
时间: 2024-11-18 20:20:06

uva 1509 Leet的相关文章

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10392 Factoring Large Numbers:素因子分解

10392 - Factoring Large Numbers Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=1333 One of the central ideas behind much cryptography is that factoring

UVa 10182 Bee Maja:规律&amp;amp;O(1)算法

10182 - Bee Maja Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1123 Maja is a bee. She lives in a bee hive with thousands of other bees. This bee hive c

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:UVa 11461 Square Numbers (简单数学)

11461 - Square Numbers Time limit: 1.000 seconds http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=24 56 A square number is an integer number whose square root is also an integer.

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVa 701 The Archeologists&#039; Dilemma: 数学及枚举

701 - The Archeologists' Dilemma Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=642 An archeologist seeking proof of the presence of extraterrestrials i

UVa 550 Multiplying by Rotation:模拟乘法

550 - Multiplying by Rotation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=491 Warning: Not all numbers in this problem are decimal numbers! Multiplic

UVA 之401 - Palindromes

A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string is read from ri