📄 morse.cpp
字号:
#include <cstdio>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
string morse[30]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","..--",".-.-","---.","----"};
int key(char a)
{
if (a=='_') return 26;
if (a==',') return 27;
if (a=='.') return 28;
if (a=='?') return 29;
return a-'A';
}
void solve()
{
string s,m;
int i,len[101];
cin >> s;
for (i=0; i<s.length(); ++i){
int k = key(s[i]);
len[i] = morse[k].length();
m += morse[k];
}
for (i=s.length()-1; i>=0; --i){
string t = m.substr(0,len[i]);
for (int j=0; j<30; ++j)
if (t.compare(morse[j])==0){
if (j==26) cout << '_';
else if (j==27) cout << ',';
else if (j==28) cout << '.';
else if (j==29) cout << '?';
else cout << (char)('A'+j);
}
m.erase(0,len[i]);
}
cout << endl;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("1068.txt","r",stdin);
#endif
int n;
while (cin >> n)
for (int i=1; i<=n; ++i){
cout << i << ": ";
solve();
}
#ifdef ONLINE_JUDGE
#else
fclose(stdin);
#endif
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -