USACO Section 1.2: Name That Number

 1 /*

 2 ID: leetcod3

 3 PROG: namenum

 4 LANG: C++

 5 */

 6 #include <iostream>

 7 #include <fstream>

 8 #include <string>

 9 #include <map>

10 #include <vector>

11 #include <set>

12 #include <algorithm>

13 #include <stdio.h>

14 #include <queue>

15 #include <cstring>

16 #include <cmath>

17 #include <list>

18 #include <cstdio>

19 #include <cstdlib>

20 #include <limits>

21 #include <stack>

22 

23 using namespace std;

24 

25 ofstream fout ("namenum.out");

26 ifstream fin ("namenum.in");

27 

28 map<char, string> tel;

29 set<string> dict;

30 

31 void dfs(vector<string> &ans, int dep, int size, string s, string cur) {

32     if (dep == size) {

33         if (dict.find(cur) != dict.end()) ans.push_back(cur);

34         return;

35     }

36     string tmp = tel[s[dep]];

37     for (int i = 0; i < tmp.size(); i++) {

38         dfs(ans, dep+1, size, s, cur+tmp[i]);

39     }

40 }

41 

42 int main() {

43     string N;

44     fin >> N;

45     tel['2'] = "ABC", tel['3'] = "DEF", tel['4'] = "GHI", tel['5'] = "JKL";

46     tel['6'] = "MNO", tel['7'] = "PRS", tel['8'] = "TUV", tel['9'] = "WXY";

47     ifstream fin("dict.txt");

48     string tmp;

49     while (getline(fin, tmp)) dict.insert(tmp);

50     //for (set<string>::iterator it = dict.begin(); it != dict.end(); it++) cout << *it << endl;

51     vector<string> ans;

52     dfs(ans, 0, N.size(), N, "");

53     if (ans.size() == 0) fout << "NONE" << endl;

54     else for (int i = 0; i < ans.size(); i++) fout << ans[i] << endl;

55     return 0;

56 }

 

你可能感兴趣的:(number)