1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| #include <bits/stdc++.h> #define ll long long #define ld long double #define N 110 #define Orz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define INF 2e18 #define rep(i,l,r) for(int i=l;i<=r;i++) #define pii pair<int,int> #define x first #define y second using namespace std; int n,m;
struct Trie{ Trie* c[26]; int ind; Trie(): ind(0){ memset(c,0,sizeof(c)); } }; Trie* root = new Trie();
int ch(char temp){ return temp-'a'; } void clear(Trie *cur){ for(int i=0;i<26;i++){ if(cur->c[i]){ clear(cur->c[i]); delete[] cur->c[i]; cur->c[i] = NULL; } } }
void insert(char *s,int num){ Trie *ptr = root; while(*s){ if(!ptr->c[ch(*s)]) ptr->c[ch(*s)] = new Trie(); ptr = ptr->c[ch(*s)]; s += 1; } ptr->ind = num; }
int find(char *s){ Trie *ptr = root; while(*s){ if(!ptr->c[ch(*s)])return 0; ptr = ptr->c[ch(*s)]; s += 1; } return ptr->ind; }
signed main(){ while(scanf("%d",&n)!=EOF){ int ans,cur_id = 1; rep(i,1,n){ char s[N];scanf("%s",s); ans = find(s); if(ans == 0){ printf("New! %d\n",cur_id); insert(s,cur_id); cur_id += 1; } else{ printf("Old! %d\n",ans); } } clear(root); } }
|