UVa 1509 - Leet

题目:给你一个小写的串,其中每个字母可以替换成k个字符(1~3),同一字母只能对应一个替换;

不同字母替换可以相同,给你一个转化后的串问是不是一个合法替换。

分析:搜索。按顺序搜索即可,每次枚举长度从1~k的所有替换。

如果当前字母未被替换,则可以任意替换,如果已经替换,只能选取相同替换向下搜索。

说明:题目只能看pdf版的o(╯□╰)o。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

char maps[26][4],temp[4];
char word[20],leet[100];

int dfs(int k, int p1, int p2 ,int l1, int l2)
{
	if (p1 == l1 && p2 == l2) return 1;
	if (p1 == l1 || p2 == l2) return 0;
	for (int i = 0 ; i < k ; ++ i)
		if (p2+i < l2) {
			/*组成匹配方案*/
			temp[0] = temp[1] = temp[2] = temp[3] = 0;
			for (int j = 0 ; j <= i ; ++ j) temp[j] = leet[p2+j];
			/*原来无方案*/
			int flag = 0;
			if (!strcmp(maps[word[p1]-'a'],"")) {
				flag = 1;
				strcpy(maps[word[p1]-'a'],temp);
			}
			if (flag || !strcmp(maps[word[p1]-'a'],temp))
				if(dfs(k,p1+1,p2+i+1,l1,l2)) return 1;
			/*回溯*/
			if (flag) strcpy(maps[word[p1]-'a'],"");
		}
	return 0;
}

int main()
{
	int n,k;
	while (cin >> n)
	while (n --) {
		cin >> k >> word >> leet;
		if (strlen(leet) > 3*strlen(word)) {
			cout << 0 << endl;
			continue;
		}

		for (int i = 0 ; i < 26 ; ++ i)
			strcpy(maps[i],"");
		cout << dfs(k,0,0,strlen(word),strlen(leet)) << endl;
	}
	return 0;
}
时间: 2024-08-28 09:48:15

UVa 1509 - Leet的相关文章

uva 1509 Leet(暴力)

uva 1509 Leet 题目大意:给你一个小写的串,其中每个字母可以替换成k个字符(1~3),同一字母只能对应一个替换:不同字母替换可以相同,给你一个转化后的串问是不是一个合法替换. 解题思路:搜索.按顺序搜索即可,每次枚举长度从1~k的所有替换.如果当前字母未被替换,则可以任意替换,如果已经替换,只能选取相同替换向下搜索. #include<stdio.h> #include<string.h> #include<algorithm> #include<st

uva 1509 - Leet(暴力)

题目链接: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,

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B

[2016-02-19][UVA][129][Krypton Factor]

UVA - 129 Krypton Factor Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description You have been employed by the organisers of a Super Krypton Factor Contest in which contestants have very high mental and physica

[2016-02-03][UVA][514][Rails]

时间:2016-02-03 22:24:52 星期三 题目编号:UVA 514 题目大意:给定若干的火车(编号1-n),按1-n的顺序进入车站, 给出火车出站的顺序,问是否有可能存在 分析:    FIFO,用栈模拟一遍即可, 方法:    根据输入的顺序,从1-n开始,当前操作的为i 如果i是当前对应的编号,那么直接跳过(进入B) 如果不是,根据当前需求的编号,小于i,就从栈顶弹出一个元素, 看这个元素是否是需求的,是则继续.否则NO 1 2 3 4 5 6 7 8 9 10 11 12 13