Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long int
- #define db(x) cout<<#x<<" -> "<<x<<endl
- struct node{
- bool endmark;
- node *next[10];
- node(){
- endmark = false;
- for(int i=0; i<10; i++){
- next[i] = 0;
- }
- }
- } *root ;
- void _insert(string str){
- node *cur = root;
- for(int i=0; i<str.size(); i++){
- int x = str[i]-'0';
- if(cur->next[x]==0 ){
- cur->next[x] = new node();
- }
- cur = cur->next[x];
- }
- cur->endmark = true;
- }
- bool _search(string str){
- node *cur = root;
- for(int i=0; i<str.size(); i++){
- int x = str[i]-'0';
- // db(x);
- // db(cur->endmark);
- if(cur->next[x] == 0 ){return false ; }
- cur = cur->next[x];
- if(cur->endmark) {return true; }
- }
- return false;
- }
- void del(node *cur){
- for(int i=0; i<10; i++){
- if(cur->next[i]){
- del(cur->next[i]);
- }
- }
- delete(cur);
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- int t;
- cin>>t;
- int tc = 0;
- while(t--){
- root = new node();
- string str;
- int n;
- cin>>n;
- vector< string > vec;
- bool f = false;
- for(int i=0; i<n; i++){
- cin>>str;
- vec.push_back(str);
- // cout<<endl;
- // if(_search(str)== true ){
- // f = true;
- // printf("Case %d: No\n",++tc);
- // break;
- // }
- // _insert(str);
- }
- sort(vec.begin(),vec.end());
- for(int i=0; i<vec.size(); i++){
- if(_search(vec[i]){ flag = true ; break; }
- _insert(vec[i]);
- }
- del(root);
- if(flag) {printf("Case %d: NO\n"); continue; }
- printf("Case %d: YES\n",++tc);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement