码迷,mamicode.com
首页 > 其他好文 > 详细

Name That Number

时间:2016-05-12 23:59:19      阅读:392      评论:0      收藏:0      [点我收藏+]

标签:

  

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;
}




Name That Number

标签:

原文地址:http://blog.csdn.net/achenkui/article/details/51346217

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!