电话机上每一个数字下面都写了若干个英文字母。分布如下:
1~abc
2~def
3~ghi
4~ikl
5~mn
6~opq
7~rst
8~uvw
9~xyz
现在给定一个单词表和一串数字密码,请你用单词表中的单词翻译这个密码。
标签:
8
73373711664
thi
shs
this
is
b
a
boo
k
thi shs b boo k
分析:
先把单词映射到数字,再暴力匹配即可
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) const int maxn=1e2+10; const int dis[][2]={0,1,-1,0,0,-1,1,0}; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;} string a[maxn],f,b[maxn]; int len[maxn],len1,n; string check(string a,int l) { string p; for(int i=0;i<l;i++) { if(a[i]==‘a‘||a[i]==‘b‘||a[i]==‘c‘)p+=‘1‘; else if(a[i]==‘d‘||a[i]==‘e‘||a[i]==‘f‘)p+=‘2‘; else if(a[i]==‘g‘||a[i]==‘h‘||a[i]==‘i‘)p+=‘3‘; else if(a[i]==‘j‘||a[i]==‘k‘||a[i]==‘l‘)p+=‘4‘; else if(a[i]==‘m‘||a[i]==‘n‘)p+=‘5‘; else if(a[i]==‘p‘||a[i]==‘q‘||a[i]==‘o‘)p+=‘6‘; else if(a[i]==‘r‘||a[i]==‘s‘||a[i]==‘t‘)p+=‘7‘; else if(a[i]==‘u‘||a[i]==‘v‘||a[i]==‘w‘)p+=‘8‘; else if(a[i]==‘x‘||a[i]==‘y‘||a[i]==‘z‘)p+=‘9‘; } return p; } void dfs(int now,string ans) { if(now==len1){cout<<ans.substr(1,ans.length()-1)<<endl;exit(0);} else { for(int i=0;i<n;i++)if(now+len[i]<=len1&&b[i]==f.substr(now,len[i]))dfs(now+len[i],ans+" "+a[i]); return; } } int main() { int i,j,m,k,t; scanf("%d",&n); cin>>f;len1=f.length(); rep(i,0,n-1)cin>>a[i],len[i]=a[i].length(),b[i]=check(a[i],len[i]); dfs(0,""); puts("No Solutions!"); //system("pause"); return 0; }
标签:
原文地址:http://www.cnblogs.com/dyzll/p/5645445.html