Advertisement
Manioc

Morse

Feb 6th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string frase;
  6. map<char, string> morse;
  7.  
  8. struct node{
  9.     bool end;
  10.     string fim;
  11.     struct node* word[3];
  12. } *head;
  13.  
  14. void init(){
  15.     head = new node();
  16.     head->end = false;
  17.     head->fim = "";
  18. }
  19.  
  20. void add(string palavra){
  21.     node* current = head;
  22.     for(int i = 0; i < palavra.size(); i++){
  23.         for(int j = 0; j < morse[palavra[i]].size(); j++){
  24.             int letra = morse[palavra[i]][j]-'-';
  25.            
  26.             if(current->word[letra] == NULL) current->word[letra] = new node();
  27.            
  28.             current = current->word[letra];
  29.         }
  30.     }
  31.     current->end = true;
  32.     current->fim = palavra;
  33. }
  34.  
  35. bool search(string codigo){
  36.     node* current = head;
  37.     for(int j = 0; j < codigo.size(); j++){
  38.         int letra = codigo[j]-'-';
  39.            
  40.         if(current->word[letra] == NULL) return false;
  41.            
  42.         current = current->word[letra];
  43.     }
  44.     if(current->end){
  45.         //cout << current-> fim << " " << frase << endl;
  46.         if(frase != "") frase += " ";
  47.         frase += current->fim;
  48.         return true;
  49.     }
  50.     return false;
  51. }
  52. int main(){
  53.     ios::sync_with_stdio(false);
  54.     for(int i = 0; i < 26; i++){
  55.         char letra;
  56.         string code; cin >> letra >> code;
  57.         morse[letra] = code;
  58.     }
  59.     init();
  60.     int num; cin >> num;
  61.     for(int i = 0; i < num; i++){
  62.         string palavra; cin >> palavra;
  63.         add(palavra);
  64.     }
  65.     while(true){
  66.         int cases; cin >> cases;
  67.         if(!cases)break;
  68.        
  69.         bool flag = true;
  70.         string notin = "";
  71.         frase = "";
  72.         for(int i = 0; i < cases; i++){
  73.             string codigo; cin >> codigo;
  74.             if(flag) flag &= search(codigo);
  75.             if(!flag && notin == "") notin = codigo;
  76.         }
  77.        
  78.         if(flag){
  79.             cout << frase << endl;
  80.         }else cout << notin << " not in dictionary.\n";
  81.     }
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement