Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long int ll;
- #define FILEIO freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
- const int N=5e5+99;
- struct node
- {
- struct node* nxt[26];
- int cnt=0;
- };
- node* head=new node();
- void clear1(node *nd)
- {
- for(int i=0;i<26;i++)
- if(nd->nxt[i]!=NULL) clear1(nd->nxt[i]);
- free(nd);
- }
- void insert(string s)
- {
- node* nd=head;
- for(int i=0;i<s.length();i++)
- {
- int k=s[i]-'A';
- if(nd->nxt[k]==NULL)nd->nxt[k]=new node();
- nd=nd->nxt[k];
- nd->cnt++;
- }
- }
- int ans=0;
- int dfs(node* nd)
- {
- int k=0;
- for(int i=0;i<26;i++)
- {
- if(nd->nxt[i]==NULL) continue;
- k+=dfs(nd->nxt[i]);
- }
- int kk=(nd->cnt)-k;
- if(kk>=2) k+=2,ans+=2;
- return k;
- }
- int32_t main()
- {
- #ifndef ONLINE_JUDGE
- FILEIO
- #endif
- int t0;cin>>t0;
- for( int tc=1;tc<=t0;tc++)
- {
- clear1(head);
- head=new node();
- int n; ans=0;
- cin>>n;
- for(int i=0;i<n;i++)
- {
- string s;cin>>s;
- reverse(s.begin(),s.end());
- insert(s);
- }
- dfs(head);
- cout<<"Case #"<<tc<<": "<<ans<<endl;
- }
- }
Add Comment
Please, Sign In to add comment