Advertisement
daskalot

Untitled

Mar 22nd, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <string>
  3. #define f first
  4. #define s second
  5. #define pb push_back
  6. #define mp make_pair
  7. #define INF (1 << 27)
  8. #define EPS 0.00000000001
  9. using namespace std;
  10. int N;
  11. string cont[50];
  12. typedef struct TrieNode{
  13. int prefixes;
  14. int words;
  15. bool isEndOfWord;
  16. struct TrieNode *child[26];
  17. };
  18. TrieNode* createNode(){
  19. TrieNode *node = new TrieNode();
  20. node->prefixes = 0;
  21. node->isEndOfWord = false;
  22. node->words = 0;
  23. for (int i=0; i < 26; i++) node->child[i] = NULL;
  24. return node;
  25. };
  26. void insertWord(TrieNode *root, string word){
  27. TrieNode *current = root;
  28. for (int i=0; i < word.size(); i++){
  29. int index = word[i] - 'a';
  30. if (!current->child[index]) current->child[index] = createNode();
  31. current->prefixes++;
  32. current = current->child[index];
  33. }
  34. current->isEndOfWord = true;
  35. current->words++;
  36. }
  37. void printShortest(int tmpIdx, TrieNode* root){
  38. for(int i=0;i<cont[tmpIdx].size();i++){
  39. char tmpChar = cont[tmpIdx][i];
  40. if(root->prefixes != 1) cout << cont[tmpIdx][i];
  41. else break;
  42. root = root->child[cont[tmpIdx][i]-'a'];
  43. }
  44. cout << endl;
  45. }
  46. int main(){
  47. TrieNode* root = createNode();
  48. cin >> N;
  49. for(int i=0;i<N;i++){
  50. string tmp;
  51. cin >> tmp;
  52. cont[i] = tmp;
  53. insertWord(root,tmp);
  54. }
  55. if(N==1){
  56. cout << cont[0][0] << endl;
  57. return 0;
  58. }
  59. for(int i=0;i<N;i++) printShortest(i,root);
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement