Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- char s[10005][12];
- bool flag = true;
- struct node
- {
- bool endmark;
- node *next[12];
- node()
- {
- endmark = false;
- for(int i=0; i<10; i++)
- {
- next[i] = NULL;
- }
- }
- }*root;
- void Insert(char *str,int len)
- {
- node *curr = root;
- for(int i=0; i<len; i++)
- {
- int id = str[i] - '0';
- if(curr->next[id]==NULL)
- {
- curr->next[id] = new node();
- }
- curr = curr->next[id];
- }
- curr->endmark = true;
- }
- bool Search(char *str,int len)
- {
- node *curr = root;
- int i;
- for(i=0; i<len-1; i++)
- {
- int id = str[i] - '0';
- curr = curr->next[id];
- if(curr->endmark==true)
- {
- flag = false;
- }
- }
- }
- void del(node *curr)
- {
- for(int i=0; i<10; i++)
- {
- if(curr->next[i])
- {
- del(curr->next[i]);
- }
- }
- delete(curr);
- }
- int main()
- {
- int num,i,a,test,len;
- scanf("%d",&test);
- while(test--)
- {
- root = new node();
- scanf("%d",&num);
- for(i=1; i<=num; i++)
- {
- scanf("%s",s[i]);
- len = strlen(s[i]);
- Insert(s[i],len);
- }
- flag = true;
- for(i=1;i<=num;i++)
- {
- len = strlen(s[i]);
- Search(s[i],len);
- }
- if(flag)
- {
- printf("YES\n");
- }
- else
- {
- printf("NO\n");
- }
- del(root);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment