USACO SEC.1.2 No.3 Name That Number

题意:给定一个数字(最长12),每个数字字符对应3个字母映射,再给定一个字典,求出对应数字所有字典当中存在的单词

解法:普通解法:给出数字所有对应的单词,对每个单词查字典

        好的解法:反查字典,对应字典中的单词映射为数字,与输入进行比较(官方题解)

 

普通枚举法如下

/*
ID: lsswxr1
PROG: namenum
LANG: C++
*/
#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <string>
#include <fstream>
using namespace std;

///宏定义
const int  INF = 1000000000;
const int MAXN = 15;
const int maxn = MAXN;
///全局变量 和 函数

#define USACO
#ifdef USACO
#define cin fin
#define cout fout
#endif
//////////////////////////////////////////////////////////////////////////
set<string> dict;
set<string> result;
string num;
int len;
char reflect[][3] = {{'A', 'B', 'C'}, {'A', 'B', 'C'}, 
    {'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'I'}, 
    {'J', 'K', 'L'}, {'M', 'N', 'O'}, {'P', 'R', 'S'}, 
    {'T', 'U', 'V'}, {'W', 'X', 'Y'},  
};
int cnt;
void dfs(int cur, string& temp)
{
    if (cur == len)
    {
        if (dict.count(temp) != 0)
        {
            cnt++;
            result.insert(temp);
        }
        return;
    }
    for (int i = 0; i < 3; i++)
    {
        if (cur == 0)
        {
            temp = "";
        }
        temp += reflect[num[cur] - '0'][i];
        dfs(cur + 1, temp);
        temp.erase(temp.length() - 1);  //回溯
    }
}
int main()
{
#ifdef USACO    
    ofstream fout ("namenum.out");
    ifstream fin ("namenum.in");
#endif    
    ///变量定义
    ifstream fin1("dict.txt");
    string tmp;
    dict.clear();
    while (fin1 >> tmp)
    {
        dict.insert(tmp);
    }
    while (cin >> num)
    {
        result.clear();
        len = num.size();
        cnt = 0;
        string myStr = "";
        dfs(0, myStr);
        set<string>::iterator pResult = result.begin();
        while (pResult != result.end())
        {
            cout << *pResult << endl;
            pResult++;
        }
        if (cnt == 0)
            cout << "NONE" << endl;
    }

    ///结束
    return 0;
}

 更好的解法

/*
ID: lsswxr1
PROG: namenum
LANG: C++
*/
#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <string>
#include <fstream>
using namespace std;

///宏定义
const int  INF = 1000000000;
const int MAXN = 15;
const int maxn = MAXN;
///全局变量 和 函数

#define USACO
#ifdef USACO
#define cin fin
#define cout fout
#endif
//////////////////////////////////////////////////////////////////////////
int refTable[330];
int main()
{


#ifdef USACO    
    ofstream fout ("namenum.out");
    ifstream fin ("namenum.in");
#endif    
    ///变量定义
    ifstream fin1("dict.txt");
    refTable['A' - '0'] = refTable['B' - '0'] = refTable['C' - '0'] = 2;
    refTable['D' - '0'] = refTable['E' - '0'] = refTable['F' - '0'] = 3;
    refTable['G' - '0'] = refTable['H' - '0'] = refTable['I' - '0'] = 4;
    refTable['J' - '0'] = refTable['K' - '0'] = refTable['L' - '0'] = 5;
    refTable['M' - '0'] = refTable['N' - '0'] = refTable['O' - '0'] = 6;
    refTable['P' - '0'] = refTable['R' - '0'] = refTable['S' - '0'] = 7;
    refTable['T' - '0'] = refTable['U' - '0'] = refTable['V' - '0'] = 8;
    refTable['W' - '0'] = refTable['X' - '0'] = refTable['Y' - '0'] = 9;
    string num;
    while (cin >> num)
    {
        int cnt = 0;
        string temp;
        while (fin1 >> temp)
        {
            string thisStr = temp;
            for (int i = 0; i < temp.size(); i++)
            {
                char cur = temp[i];
                int pos = cur - '0';
                int ans = refTable[pos];
                thisStr[i] = ans + '0';
            }
            if (thisStr == num)
            {
                cnt++;
                cout << temp << endl;
            }
        }
        if (cnt == 0)
            cout << "NONE" << endl;
    }

    ///结束
    return 0;
}

 

posted on 2013-11-19 21:04  小书包_Ray  阅读(150)  评论(0编辑  收藏  举报

导航