2016年1月10日 星期日

NPSC2015-J2G-morse 國中決 G.光通訊

/*
npsc2015-j2g-morse
sol-1 every char check
*/

#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;
const int MaxN = 1000;
int mos[]={12,2111,2121,211,1,  1121,221,1111,11,1222,  212,1211,22,21,222,   1221,2212,121,111,2,  112,1112,122,2112,2122, 2211};
//A  B    C    D   E    F   G    H   I   J      K   L   M  N   O      P   Q     R  S   T    U   V    W   X   Y     Z
char tbl[81];  // A=1+2*3=4 , B=2+1*3+1*9+1*27=41 , J=1+2*3+2*9+2*27;
int main(void)
{
int i,j,k , n;
int p[]={1,3,9,27};
int v,d,e,f;
string s;
// gen tbl
for(i=0; i<26; ++i)
{
k=mos[i];
f=v=0;
e=1000;
while(k>0)
{
j=k/e;
k%=e;
e/=10;
if(j>0) v+=(j*p[f++]);

}
tbl[v] = char(i+'A');
// cout <<char(i+'A') << v << endl;
}


while(cin >>n)  //cin >> n;
{
cin >> s;
v=d=e=f=0;
for(j=0; j<n; ++j)
{
if( s[j] == '.' )
{
++d;
if(e>0)
{
v+=p[f];
if(e==3) v+=p[f];
e=0;
}

}
else // ==
{
++e;
if(d==0) continue;
if(d==1) ++f;
else
{
cout << tbl[v]; //cout << v << " ";
if(d==7) cout << " ";
v=0;
f=0;
}
d=0;
}
}
// last char
if(d==1) ++f;
v+=p[f];
if(e==3) v+=p[f];
cout << tbl[v]<< endl;  // << v << endl;

}

return 0;
}
/*
69
===.=...=.===.===.=...=.=.=...===.=.===.=.......===.===.=...===.===.=
57
=.===...===.=.=.=...===.=.===.=...===.=.=...=...=.=.===.=
53
===.===.=...=.=.=.=...=.=...=.===.===.===...===.=.===
55
=.===.=.=...===.===...===.=...===.===.===...=.===.===.=
47
===.===.=.===...=.===.=...=.=.=...===...=.=.===
65
=.=.=.===...=.===.===...===.=.=.===...===.=.===.===...===.===.=.=

--------out
NPSC GG
ABCDEF
GHIJK
LMNOP
QRSTU
VWXYZ

*/

0 意見:

張貼留言