#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include < string>using namespace Std;const int M = 26;int n; #define MAXN 1005char s[maxn][21];struct node{int v; Node *next[m];} *root;void creat (char *s)//allocating memory is best used calloc because it initializes and malloc uninitialized is random data garbage {int len = strlen (s); Node *p = root, *q; for (int i=0; i<len; i++) {int id = s[i]-' a '; cout<<id<< ""; if (p->next[id] = = NULL) {q = (node *) calloc (1, sizeof (node)); Q->v = 1; P->next[id] = q; p = q; } else {p = p->next[id]; p->v++; }}}void Find (char *s) {int len = strlen (s); Node *p = root; for (int i=0; i<len; i++) {int id = s[i]-' a '; p = p->next[id]; printf ("%c", S[i]); if (p->v = = 1) break; } Cout<<endl;} int main () {n = 0; RooT = (node *) calloc (1, sizeof (node)); Root node//root = new node; while (~SCANF ("%s", S[n]) {creat (s[n++]); } for (int i=0; i<n; i++) {printf ("%s", S[i]); Find (S[i]); } return 0;}
POJ 2001 Phone