Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. struct vertex
  5. {
  6.     int next[300];
  7.     bool leaf;
  8. };
  9. vertex t [210];
  10. int sz=1;
  11. string s;
  12. void add_string (const string & s)
  13. {
  14.     int v = 0;
  15.     for (size_t i=0; i<s.length(); ++i) {
  16.         char c = s[i]-'a';
  17.         if (t[v].next[c] == -1) {
  18.             for (int i=0;i<29;++i)
  19.             {
  20.                 t[sz].next[i]=-1;
  21.             }
  22.             t[v].next[c] = sz++;
  23.         }
  24.         v = t[v].next[c];
  25.     }
  26.     t[v].leaf = true;
  27. }
  28. bool pref(string s)
  29. {
  30.     int v=0;
  31.     for (int i=0;i<s.length();++i)
  32.     {
  33.         char c=s[i]-'a';
  34.         if (t[v].next[c]==-1)
  35.         {
  36.             return false;
  37.         }
  38.         v=t[v].next[c];
  39.     }
  40.     return true;
  41. }
  42. int main()
  43. {
  44.     int n,k,t;
  45.     string a[200] ;
  46.     cin>>t;
  47.     for (int g=0;g<2*t-2;++g)
  48.     {
  49.         cin>>a[g];
  50.         if (a[g].length()==t-1) s=a[g];
  51.     }
  52.     for (int i=0;i<29;++i)
  53.     {
  54.         t[0].next[i]=-1;
  55.     }
  56.     add_string(s);
  57.     for (int g=0;g<2*t-2;++g)
  58.     {
  59.         if (pref(a[g])) {cout<<'P';} else {cout<<'S';}
  60.     }
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement