模拟
//zoj1068 #include <iostream> #include <string> using namespace std; const int maxn=102; int N,number[maxn],transnum[26]={2,4,4,3,1,4,3,4,2,4,3,4,2,2,3,4,4,3,3,1,3,4,3,4,4,4}; string st,code,translate[26]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."}; void init() { number[0]=0; code=""; } string encoder(string st,int *num) { string ans=""; int len=st.length(); for (int i=0;i<len;i++) { num[0]++; if (st[i]>='A'&&st[i]<='Z') { ans+=translate[st[i]-65]; num[num[0]]=transnum[st[i]-65]; continue; } switch (st[i]) { case '_' :ans+="..--"; break; case '?' :ans+="----"; break; case ',' :ans+=".-.-"; break; case '.' :ans+="---."; break; } num[num[0]]=4; } return ans; } void decoder(string code,int *num) { string cut; for (int i=num[0];i>=1;i--) { cut.erase(); cut.insert(0,code,0,num[i]); code.erase(0,num[i]); for (int j=0;j<26;j++) if (translate[j]==cut) cout<<char(j+65); if (cut=="..--") cout<<"_"; if (cut=="----") cout<<"?"; if (cut==".-.-") cout<<","; if (cut=="---.") cout<<"."; } } int main() { // freopen("t.txt","r",stdin); cin>>N; getchar(); for (int i=1;i<=N;i++) { init(); getline(cin,st); code=encoder(st,number); printf("%d: ",i); decoder(code,number); cout<<endl; } return 0; }