Advertisement
Eather

Prefix Span: Code

Jun 24th, 2011
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.02 KB | None | 0 0
  1.                        /*Bismillahir Rahmanur Rahim*/
  2. //Template created by topcoder00
  3.  
  4. # include <list>
  5. # include <bitset>
  6. # include <algorithm>
  7. # include <sstream>
  8. # include <iostream>
  9. # include <cstdlib>
  10. # include <ctime>
  11. # include <set>
  12. # include <map>
  13. # include <cmath>
  14. # include <queue>
  15. # include <limits>
  16. # include <stack>
  17. # include <vector>
  18. # include <cstring>
  19. # include <cstdio>
  20. # include <fstream>
  21. using namespace std;
  22.  
  23. # define FOR(i, a, b) for (int i=a; i<b; i++)
  24. # define REP(i, a) FOR(i,0,a)
  25. # define all(c) (c).begin(), (c).end()
  26. # define sz(x) x.size()
  27. # define pb push_back
  28. # define UNQ(s) {sort(all(s));(s).erase(unique(all(s)),s.end());}
  29. # define VS vector<string>
  30.  
  31. VS mseq;
  32. map<char,int>mpc ;
  33. int min_sup;
  34. int cou=0;
  35. VS prefix;
  36.  
  37. void count1(string s,VS seq)
  38. {
  39.     for(int k=0;k<sz(s);k++){
  40.     REP(i,sz(seq))
  41.     {
  42.         REP(j,sz(seq[i]))
  43.         {
  44.             if(seq[i][j]==s[k])
  45.             {
  46.                 mpc[s[k]]++;break;
  47.             }
  48.         }
  49.     }
  50.     }
  51. }
  52. map<string,int>mpvc;
  53.  
  54. void count2(VS vs, VS seq)
  55. {
  56.     for(int k=1;k<sz(vs);k++)
  57.     {
  58.         REP(i,sz(seq))
  59.         {
  60.             REP(j,sz(seq[i]))
  61.             {
  62.                 if(seq[i].substr(j,sz(vs[k]))==vs[k])
  63.                 {
  64.                     if(sz(vs[k])==1&&seq[i][j-1]!='_')
  65.                     {
  66.                         int p=j;
  67.                         while(1)
  68.                         {
  69.                             p--;
  70.                             if(p==0 || seq[i][p]=='_' || seq[i][p]=='(' || seq[i][p]==' ')break;
  71.                         }
  72.                         if(seq[i][p]!='_')
  73.                         {
  74.                             mpvc[vs[k]]++;
  75.                             break;
  76.                         }
  77.                     }
  78.                     else if(sz(vs[k])>1) {mpvc[vs[k]]++;break;}
  79.                 }
  80.             }
  81.         }
  82.     }
  83. }
  84. VS fsp;
  85. map<string,bool>mp;
  86.  
  87. void prin();
  88.  
  89. void mainkaj(string s)
  90. {
  91.     string ma=s;
  92.     for(int i=0;i<sz(s);i++)
  93.     {
  94.         if(s[i]=='('){s[i]=' ';
  95.            while(s[i]!=')')i++;s[i]=' ';}
  96.         else s.insert(i," "),i++;
  97.     }
  98.  
  99.     VS pr, projected;
  100.     pr.clear();projected.clear();
  101.  
  102.     string test="";
  103.     stringstream ss(s);
  104.  
  105.     while(ss>>test)
  106.     {
  107.         pr.pb(test);
  108.     }
  109.     s.clear();
  110.     for(int i=0;i<sz(pr);i++)s+=pr[i];
  111.  
  112.     REP(i,sz(mseq))
  113.     {
  114.         int br=0;string proj=""; int k=0;
  115.  
  116.         REP(j,sz(mseq[i]))
  117.         {
  118.             if(mseq[i].substr(j,sz(pr[k]))==pr[k])
  119.             {
  120.                 if(br==1 && k!=(sz(pr)-1))
  121.                 {
  122.                     while(mseq[i][j]!=')')j++;br=0;
  123.                 }
  124.                 else if(br==1 && k==(sz(pr)-1))
  125.                 {
  126.                     j+=(sz(pr[k])-1);
  127.                     if(mseq[i][j+1]==')')j+=1;
  128.                     else if(mseq[i][j+1]!=')' && mseq[i][j+1]!=' ')
  129.                         proj.pb('_'),proj.pb(pr[sz(pr)-1][sz(pr[sz(pr)-1])-1]);
  130.                     br=0;
  131.                 }
  132.                 else if(br==0)j+=1;
  133.                 k++;
  134.             }
  135.             if(mseq[i][j]=='(')br=1;
  136.  
  137.             if(k==sz(pr))
  138.             {
  139.                 proj+=mseq[i].substr(j+1);
  140.                 bool c=0;
  141.                 while(mseq[i][j]!='>'){j++;if(isalpha(mseq[i][j]))c=1;}
  142.                 if(c==0)proj.clear();
  143.                 break;
  144.             }
  145.         }
  146.         if(sz(proj)>1)projected.pb(proj);
  147.     }
  148.  
  149. /*    cout<<"Projected Database:"<<endl;
  150.     REP(i,sz(projected))
  151.         cout<<projected[i]<<endl;
  152. */
  153.     VS frst;frst.clear();
  154.  
  155.     REP(i,sz(projected))
  156.     {
  157.         string tt="";
  158.         REP(j,sz(projected[i]))
  159.         {
  160.             if(projected[i][j]=='_' && isalpha(projected[i][j+1]))
  161.             {
  162.                 if(isalpha(projected[i][j+2]))tt+=projected[i].substr(j+1,j+2);
  163.             }
  164.             else if(isalpha(projected[i][j]))tt.pb(projected[i][j]);
  165.             frst.pb(tt);tt="";
  166.         }
  167.     }
  168.  
  169.     UNQ(frst); mpvc.clear();count2(frst,projected);
  170.  
  171. /*    cout<<"Frequency of members\n";
  172.     REP(i,sz(frst))
  173.     {
  174.       if(mpvc[frst[i]]>0)cout<<frst[i]<<" "<<mpvc[frst[i]]<<endl;
  175.     }
  176. */
  177.     REP(i,sz(frst))
  178.     {
  179.         string fp=ma;
  180.  
  181.         if(mpvc[frst[i]]>=min_sup)
  182.         {
  183.             if(fp[sz(fp)-1]==frst[i][0] && sz(frst[i])>1)
  184.             {
  185.                 fp.erase(sz(fp)-1);
  186.                 fp.pb('(');
  187.                 fp+=frst[i];
  188.                 fp.pb(')');
  189.             }
  190.             else fp+=frst[i];
  191.             fsp.pb(fp);
  192.         }
  193.     }
  194.       prin();
  195. }
  196.  
  197. void prin()
  198. {
  199.     for(int i=0;i<sz(fsp);i++)
  200.     {
  201.         string t;
  202.         stringstream os(fsp[i]);
  203.         string p="";
  204.         while(os>>t)p+=t;
  205.         if(mp[p]==0 && sz(p)>1)
  206.             cout<<"\n"<<fsp[i]<<endl,mp[p]=1,mainkaj(p);
  207.     }
  208. }
  209.  
  210. int main()
  211. {
  212.  
  213. //freopen("prefixspan_in.txt","r",stdin);
  214. freopen("prefixspan_out.txt","w",stdout);
  215.  
  216.     int br=0;
  217.     string frst;
  218.     while(1)
  219.     {
  220.         string s;
  221.         getline(cin,s);
  222.         if(sz(s)==0)br++;
  223.         if(br==2)break;
  224.         if(sz(s)>1)br=0;
  225.  
  226.         for(int i=0;i<sz(s);i++)
  227.         {
  228.             if(isalpha(s[i]))frst.pb(s[i]);
  229.             if(s[i]=='<')
  230.             s.insert(i+1," ");
  231.             else if(s[i]=='(')
  232.             {
  233.                 while(s[i]!=')'){if(isalpha(s[i]))frst.pb(s[i]);i++;}s.insert(i+1," ");
  234.             }
  235.             else if(s[i]!=' ')s.insert(i+1," ");
  236.         }
  237.         mseq.pb(s);
  238.     }
  239.  
  240.     cout<<"Mininum support?";cin>>min_sup;
  241.  
  242.     UNQ(frst);count1(frst,mseq);
  243.  
  244.     REP(i,sz(frst))
  245.     {
  246.         if(mpc[frst[i]]<min_sup)frst.erase(i);
  247.     }
  248.  
  249.     REP(i,sz(frst)){string tt;tt.pb(frst[i]);prefix.pb(tt);tt.clear();}
  250.     string line="\n-------------------------------------------------------------\n";
  251.     REP(i,sz(prefix)){cout<<line<<"Prefix of ("<<prefix[i]<<") are below:"<<line;mainkaj(prefix[i]);mpc.clear();}
  252.     cout<<"\n\n\n\n\n\n\n\n\n\nDesigned by Iftekhar ahmed eather,\nStudent of CSE (07 batch),\nBUBT, Mirpur";
  253.     return 0;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement