Advertisement
Rana_093

Trie_2

Nov 3rd, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long int
  5. #define db(x) cout<<#x<<" -> "<<x<<endl
  6. #define pb push_back
  7. #define mp make_pair
  8.  
  9. const int mod = (int)1e9+7;
  10.  
  11. struct node{
  12.     int  endmark;
  13.     node *next[5];
  14.     node(){
  15.         endmark = 0;
  16.         for(int i=0; i<5; i++){
  17.             next[i] = 0;
  18.         }
  19.     }
  20. } *root ;
  21.  
  22. map< string , int > MAP;
  23. int mx ;
  24.  
  25. void   _insert(string str){
  26.     node *cur = root;
  27.     int cnt = 0 ;
  28.     string s="";
  29.     for(int i=0; i<str.size(); i++){
  30.         char ch  = str[i];
  31.         int x;
  32.         if(ch=='A') {x = 0 ; }
  33.         else if(ch=='C') {x = 1 ; }
  34.         else if(ch == 'G') {x = 2 ; }
  35.         else if(ch == 'T'){x = 3 ; }
  36.         if(cur->next[x]==0 ){
  37.             cur->next[x] = new node();
  38.         }
  39.         cur = cur->next[x];
  40.         cur->endmark++;
  41.         int temp = (i+1)*cur->endmark;
  42.         mx = max(mx,temp);
  43.     }
  44. }
  45.  
  46. void del(node *cur){
  47.     for(int i=0; i<5; i++){
  48.         if(cur->next[i]){
  49.             del(cur->next[i]);
  50.         }
  51.     }
  52.     delete(cur);
  53. }
  54.  
  55. bool cmp(string str1,string str2){
  56.     return str1.size()<str2.size() ;
  57.     return str1>str2;
  58. }
  59.  
  60. int main()
  61. {
  62.     ios_base::sync_with_stdio(false);
  63.     cin.tie(0);
  64.     int t;
  65.     cin>>t;
  66.     int tc  = 0;
  67.     while(t--){
  68.         root = new node();
  69.         string str;
  70.         int n;
  71.         cin>>n;
  72.         vector< string > vec;
  73.         map<string,int>::iterator it;
  74.         bool flag = false;
  75.         for(int i=0; i<n; i++){
  76.             cin>>str;
  77.             vec.push_back(str);
  78.         }
  79.         //sort(vec.begin(),vec.end(),cmp);
  80.         mx = -1000000000;
  81.         for(int i=0; i<vec.size(); i++){
  82.                 _insert(vec[i] );
  83.         }
  84.         printf("Case %d: %d\n",++tc,mx);
  85.         del(root);
  86.     }
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement