Name That Number

http://train.usaco.o??rg/usacoprob2?a=bSpzketQQn2&S=namenum

题目大意:

?输入一个数字序列,每个数字可能

对应3个字母中的一个,即如果数字序列长度为n,会有3^n个对应的字母序列,从这3^n个字母序列中找出在dict.txt中有的序列。

<pre name="code" class="cpp">#include <iostream>
#include <fstream>
using namespace std;
ifstream fin1("dict.txt");
ifstream fin("namenum.in");
ofstream fout("namenum.out");
string dict[5005];
string let[10] = {"", "", "ABC", "DEF", "GHI", "JKL", "MNO", "PRS", "TUV", "WXY"};
string a, b;		//a,输入的编号;b检查的编号
bool dfs(int n, int m) //n,a中第几个数, m,dict中的第几个
{
	if(n == a.size() && dict[m].size() == a.size())
	{
		fout << b << endl;			//可能对应多解
		return true;
	}
	int t, k, j = m;
	bool f = false;
	t = a[n] - '0';			//从a中取出编号
	for(int i = 0; i < 3; i++) //一个数字对应3个字母
	{
		for(; j < 5005; j++) //从dict的第m个开始查找
		{
			if(dict[j].size() != a.size()) continue;		//注意不要越界
			if(dict[j][n] == let[t][i])
			{
				for(k = 0; k < n; k++)
					if(dict[j][k] != b[k]) break;			//检查前n - 1个
				if(k == n)
				{
					b[n] = let[t][i];
					if(dfs(n + 1, j))
					{
						f = true;
						break;
					}
				}
			}
			else if(dict[j][n] > let[t][i]) break;
		}
	}
	return f;
}

int main()
{
	for(int i = 0; fin1 >> dict[i]; i++);
	while(fin >> a)
	{
		b.resize(a.size());
		if(!dfs(0, 0))	fout << "NONE" << endl;
	}
	fout.close();
	return 0;
}
				
时间: 2024-10-27 05:00:02

Name That Number的相关文章

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

实现一个函数clone,使JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制

实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number.String.Object.Array.Boolean)进行值复制. 1 /** 对象克隆 2 * 支持基本数据类型及对象 3 * 递归方法 */ 4 function clone(obj) { 5 var o; 6 switch (typeof obj) { 7 case "undefined": 8 break; 9 case "string": o = obj + &q

解决sqoop报错Invalid number; item = ITEM_UNICODE

报错栈: java.sql.SQLException: Invalid number; item = ITEM_UNICODE at com.intersys.jdbc.SysList.getInt(SysList.java:1735) at com.intersys.jdbc.CacheResultSet.getInt(CacheResultSet.java:247) at org.apache.sqoop.lib.JdbcWritableBridge.readInteger(JdbcWrit

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

Minimum Inversion Number 【线段数】

Problem DescriptionThe inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of

171. Excel Sheet Column Number

Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 static publi

hdu 5898 odd-even number 数位DP

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 716    Accepted Submission(s): 385 Problem Description For a number,if the length of continuous odd digits is even and the length

Leetcode 202. Happy Number

202. Happy Number Total Accepted: 78171 Total Submissions: 208635 Difficulty: Easy Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace t

hdu 1394 Minimum Inversion Number(这道题改日我要用线段树再做一次哟~)

Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of