Advertisement
serenomateus

E. Essay Time

Aug 25th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define DIM 8000005
  4. #define ALPHABET_SIZE 'z'-'a'+1
  5.  
  6. using namespace std;
  7.  
  8. int TRIE[DIM][ALPHABET_SIZE];
  9. bool used[DIM];
  10. int sz = 1;
  11. vector<string> repeated;
  12.  
  13. void build();
  14. void add(string s);
  15.  
  16. int main(){
  17.     ios_base::sync_with_stdio(false);
  18.     cin.tie(NULL);
  19.     int n;
  20.     string s;
  21.  
  22.     build();
  23.     cin >> n;
  24.     while(n--){
  25.         cin >> s;
  26.         if(s.length()>=4)
  27.             add(s);
  28.     }
  29.     if(repeated.empty()){
  30.         cout << "SAFO\n";
  31.     }else{
  32.         cout << repeated.size() << "\n";
  33.         for(int i=0;i<repeated.size();i++){
  34.             cout << repeated[i] << "\n";
  35.         }
  36.     }
  37.  
  38.     return 0;
  39. }
  40.  
  41. void build(){
  42.     for(int i=0;i<DIM;i++){
  43.         used[i] = 0;
  44.         for(int j=0;j<ALPHABET_SIZE;j++){
  45.             TRIE[i][j] = -1;
  46.         }
  47.     }
  48.     return;
  49. }
  50.  
  51. void add(string s){
  52.     int node = 0;
  53.     for(int i=0;i<s.length();i++){
  54.         if(TRIE[node][s[i]]==-1){
  55.             TRIE[node][s[i]] = sz++;
  56.         }
  57.         node = TRIE[node][s[i]];
  58.     }
  59.     if(used[node]){
  60.         repeated.push_back(s);
  61.     }
  62.     used[node] = 1;
  63.     return;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement