View Code of Problem 1086

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100009;
char str[maxn][52];
struct trie
{
    int next[32];
    int cnt;
    void init()
    {
        cnt=0;
        memset(next,0,sizeof(next));
    }
}tree[maxn];
int index;
void insert(char *s)
{
    int len=strlen(s);
    int p=0;
    for(int i=0;i<len;i++)
    {
        int x=s[i]-'a';
        if(tree[p].next[x]==0)
        {
            tree[index+1].init();
            tree[p].next[x]=++index;
        }
        tree[tree[p].next[x]].cnt++;
        p=tree[p].next[x];
    }
}
void find(char *s)
{
    int len=strlen(s);
    int p=0;
    for(int i=0;i<len;i++)
    {
        int x=s[i]-'a';
        if(tree[p].cnt==1)
         {return;
         }
         else
         {
             printf("%c",s[i]);

         }
         p=tree[p].next[x];
    }
}
int main()
{
    tree[0].init();
    int index1=0;
        index=0;
    while(scanf("%s",str[index1])!=EOF)
    {
        insert(str[index1]);
        index1++;
    }
         /*int len=strlen(str[0]);
         int p=0;
         for(int i=0;i<len;i++)
         {
             int x=(int)(str[0][i]-'a');
             printf("%d %d\n",tree[p].next[x],tree[p].cnt);
              p=tree[p].next[x];
         }*/

    for(int i=0;i<index1;i++)
    {   printf("%s ",str[i]);
        find(str[i]);
        printf("\n");
    }
    return 0;
}

Double click to view unformatted code.


Back to problem 1086