Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <string>
- #define f first
- #define s second
- #define pb push_back
- #define mp make_pair
- #define INF (1 << 27)
- #define EPS 0.00000000001
- using namespace std;
- int N;
- string cont[50];
- typedef struct TrieNode{
- int prefixes;
- int words;
- bool isEndOfWord;
- struct TrieNode *child[26];
- };
- TrieNode* createNode(){
- TrieNode *node = new TrieNode();
- node->prefixes = 0;
- node->isEndOfWord = false;
- node->words = 0;
- for (int i=0; i < 26; i++) node->child[i] = NULL;
- return node;
- };
- void insertWord(TrieNode *root, string word){
- TrieNode *current = root;
- for (int i=0; i < word.size(); i++){
- int index = word[i] - 'a';
- if (!current->child[index]) current->child[index] = createNode();
- current->prefixes++;
- current = current->child[index];
- }
- current->isEndOfWord = true;
- current->words++;
- }
- void printShortest(int tmpIdx, TrieNode* root){
- for(int i=0;i<cont[tmpIdx].size();i++){
- char tmpChar = cont[tmpIdx][i];
- if(root->prefixes != 1) cout << cont[tmpIdx][i];
- else break;
- root = root->child[cont[tmpIdx][i]-'a'];
- }
- cout << endl;
- }
- int main(){
- TrieNode* root = createNode();
- cin >> N;
- for(int i=0;i<N;i++){
- string tmp;
- cin >> tmp;
- cont[i] = tmp;
- insertWord(root,tmp);
- }
- if(N==1){
- cout << cont[0][0] << endl;
- return 0;
- }
- for(int i=0;i<N;i++) printShortest(i,root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement