Saturday 11 April 2009

POJ 1051


水题。


#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
char Mcode[30][5]={
/*A-G:*/".-","-...","-.-.","-..",".","..-.","--.",
/*H-N:*/"....","..",".---","-.-",".-..","--","-.",
/*O-U:*/"---",".--.","--.-",".-.","...","-","..-",
/*V-Z:*/"...-",".--","-..-","-.--","--..",
/*_.,?*/"..--","---.",".-.-","----"
};
int len[30]={
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,
4, 4, 4, 4
};
int index(char ch){
if(ch<='Z'&&ch>='A')
return ch-'A';
switch(ch){
case '_':return 26;
case '.':return 27;
case ',':return 28;
case '?':return 29;
}
return -1;
}
char* encode(char c,int& n){
int i=index(c);
n=len[i];
return Mcode[i];
}
char decode(char* m,int n){
for(int i=0;i<30;++i){
if(n!=len[i]||strncmp(Mcode[i],m,n))
continue;
if(i<26)
return i+'A';
switch(i){
case 26: return '_';
case 27: return '.';
case 28: return ',';
case 29: return '?';
}
}
return 0;
}
int main(){
int N,n;cin>>N;
for(n=1;n<=N;++n){
char msg[128],crypt[128*4]={0},*p,*q;
vector<int> codelen;
int tmp;
cin>>msg;
for(p=msg,q=crypt;*p;++p){
strcpy(q,encode(*p,tmp));
codelen.push_back(tmp);
q+=tmp;
}
q=crypt;
cout<<n<<": ";
for(int i=codelen.size()-1;i>=0;--i){
cout<<decode(q,codelen[i]);
q+=codelen[i];
}
cout<<endl;
}
return 0;
}

No comments:

Post a Comment