对于每一个数字,我们已知按哪个键会出什么字母,那么只要先预处理好对于每个字母需要按的数字的种类和个数,以及上一次的数字是否相同(根据题意相同还要输出井号)。
已经处理好每个数字后只需要将这个数字对应到新键盘上就ok了。
Code:
#include<bits/stdc++.h>
using namespace std;
int lst,len;
int a[15];
char ch[105];
inline int read(){
int ret=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch)){ret=ret*10+ch-'0';ch=getchar();}
return ret*f;
}
int main(){
freopen("mobitel.in","r",stdin);
freopen("mobitel.out","w",stdout);
for(int i=1;i<=9;i++)a[read()]=i;//处理原键盘和新键盘
scanf("%s",ch+1);len=strlen(ch+1);
for(int i=1;i<=len;i++){
int x=0,y=0;
if(ch[i]=='a')x=2,y=1;//处理字母对应的数字,x代表原数字,y代表需要按的次数
if(ch[i]=='b')x=2,y=2;
if(ch[i]=='c')x=2,y=3;
if(ch[i]=='d')x=3,y=1;
if(ch[i]=='e')x=3,y=2;
if(ch[i]=='f')x=3,y=3;
if(ch[i]=='g')x=4,y=1;
if(ch[i]=='h')x=4,y=2;
if(ch[i]=='i')x=4,y=3;
if(ch[i]=='j')x=5,y=1;
if(ch[i]=='k')x=5,y=2;
if(ch[i]=='l')x=5,y=3;
if(ch[i]=='m')x=6,y=1;
if(ch[i]=='n')x=6,y=2;
if(ch[i]=='o')x=6,y=3;
if(ch[i]=='p')x=7,y=1;
if(ch[i]=='q')x=7,y=2;
if(ch[i]=='r')x=7,y=3;
if(ch[i]=='s')x=7,y=4;
if(ch[i]=='t')x=8,y=1;
if(ch[i]=='u')x=8,y=2;
if(ch[i]=='v')x=8,y=3;
if(ch[i]=='w')x=9,y=1;
if(ch[i]=='x')x=9,y=2;
if(ch[i]=='y')x=9,y=3;
if(ch[i]=='z')x=9,y=4;
if(x==lst)printf("#");lst=x;//lst记录上一次按的数字,相同就输出‘#’
for(int j=1;j<=y;j++)printf("%d",a[x]);//将原键盘映射到新键盘上
}putchar('\n');
return 0;
}