Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string frase;
- map<char, string> morse;
- struct node{
- bool end;
- string fim;
- struct node* word[3];
- } *head;
- void init(){
- head = new node();
- head->end = false;
- head->fim = "";
- }
- void add(string palavra){
- node* current = head;
- for(int i = 0; i < palavra.size(); i++){
- for(int j = 0; j < morse[palavra[i]].size(); j++){
- int letra = morse[palavra[i]][j]-'-';
- if(current->word[letra] == NULL) current->word[letra] = new node();
- current = current->word[letra];
- }
- }
- current->end = true;
- current->fim = palavra;
- }
- bool search(string codigo){
- node* current = head;
- for(int j = 0; j < codigo.size(); j++){
- int letra = codigo[j]-'-';
- if(current->word[letra] == NULL) return false;
- current = current->word[letra];
- }
- if(current->end){
- //cout << current-> fim << " " << frase << endl;
- if(frase != "") frase += " ";
- frase += current->fim;
- return true;
- }
- return false;
- }
- int main(){
- ios::sync_with_stdio(false);
- for(int i = 0; i < 26; i++){
- char letra;
- string code; cin >> letra >> code;
- morse[letra] = code;
- }
- init();
- int num; cin >> num;
- for(int i = 0; i < num; i++){
- string palavra; cin >> palavra;
- add(palavra);
- }
- while(true){
- int cases; cin >> cases;
- if(!cases)break;
- bool flag = true;
- string notin = "";
- frase = "";
- for(int i = 0; i < cases; i++){
- string codigo; cin >> codigo;
- if(flag) flag &= search(codigo);
- if(!flag && notin == "") notin = codigo;
- }
- if(flag){
- cout << frase << endl;
- }else cout << notin << " not in dictionary.\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement