Advertisement
Guest User

Георгий Агапов

a guest
Nov 3rd, 2010
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.39 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. using namespace std;
  4.  
  5. typedef unsigned short id;
  6. typedef unsigned char uc;
  7. typedef unsigned int ui;
  8.  
  9. const id DICT_SIZE = 10005, NONE = DICT_SIZE + 1;
  10.  
  11. ui N, M;
  12.  
  13. uc wbuf[100001];
  14. ui wbs;
  15. uc buf [900001];
  16. ui bfs;
  17.  
  18. void get_line() {
  19.     bfs = 0;
  20.     uc ch = getchar();
  21.     while (ch != '\n') {
  22.         buf[bfs] = ch;
  23.         ch = getchar();
  24.         bfs++;
  25.     }
  26. }
  27.  
  28. void get_word() {
  29.     uc ch = getchar();
  30.     while (ch != '\n') {
  31.         if (ch != 0 && ch != '\r') {
  32.             wbuf[wbs] = ch;
  33.             wbs++;
  34.         }
  35.         ch = getchar();
  36.     }
  37. }
  38.  
  39. struct pstr {
  40.     id start, end;
  41.  
  42.     pstr() {
  43.         start = end = 0;
  44.     }
  45.  
  46.     pstr(ui s, ui e) {
  47.         start = s;
  48.         end = e;
  49.     }
  50.  
  51.     int len() {
  52.         return end - start;
  53.     }
  54.  
  55.     void cut_begin(ui s) {
  56.         start = start + s;
  57.     }
  58.  
  59.     void cut_end(ui e) {
  60.         end = start + e;
  61.     }
  62.  
  63.     void print() {
  64.         for (int i = 0; i < len(); i++)putchar((*this)[i]);
  65.         putchar('\n');
  66.     }
  67.     uc & operator[](ui);
  68.     pstr & operator=(const pstr);
  69. };
  70.  
  71. uc & pstr::operator[](ui i) {
  72.     return wbuf[start + i];
  73. }
  74.  
  75. pstr& pstr::operator =(const pstr p) {
  76.     this->end = p.end;
  77.     this->start = p.start;
  78.     return *this;
  79. }
  80.  
  81. struct vert {
  82.     pstr p;
  83.     id length, parent, index, g [256];
  84.     uc ac;
  85.     bool leaf;
  86.     id & operator[](uc);
  87.     void addstr(pstr & b, id & wlen);
  88.     vert & c(uc);
  89.  
  90.     void init(id ind) {
  91.         for (id i = 0; i < 256; i++)
  92.             g[i] = NONE;
  93.         index = ind;
  94.     }
  95.  
  96.     void print() {
  97.         printf("index=%d ac='%c' parent=%d leaf=%d p: ", index, ac, parent, leaf);
  98.         p.print();
  99.     }
  100.     bool search(uc * str, ui &st, ui & size);
  101. };
  102. vert dict [DICT_SIZE + 2]; //0 - root
  103. id dinc = 1;
  104.  
  105. id & vert::operator[](uc ch) {
  106.     return g[ch];
  107. }
  108.  
  109. id create_vect() {
  110.     dict[dinc].init(dinc);
  111.     dinc++;
  112.     return dinc - 1;
  113. }
  114.  
  115. vert & vert::c(uc ch) {
  116.     return dict[g[ch]];
  117. }
  118.  
  119. void vert::addstr(pstr & b, id & wlen) {
  120.     ui i;
  121.     for (i = 0; b[i] == p[i] && i < p.len() && i < b.len(); i++);
  122.     if (i == p.len()) {//p - полностью подстрока b
  123.         if (g[b[i]] != NONE) {
  124.             if (b.len() == i) {
  125.                 leaf = true;
  126.                 length = wlen;
  127.             } else {
  128.                 id ind = g[b[i]];
  129.                 b.cut_begin(i + 1);
  130.                 dict[ind].addstr(b, wlen);
  131.             }
  132.         } else {
  133.             if (b.len() == i) {
  134.                 leaf = true;
  135.                 length = wlen;
  136.             } else {
  137.                 id n = create_vect();
  138.                 g[b[i]] = n;
  139.                 vert & v = dict[n];
  140.                 v.ac = b[i];
  141.                 v.p = b;
  142.                 v.p.cut_begin(i + 1);
  143.                 v.parent = index;
  144.                 v.leaf = true;
  145.                 v.length = wlen;
  146.             }
  147.         }
  148.     } else {
  149.         id n = create_vect();
  150.         dict[parent][ac] = n;
  151.         vert & nv = dict[n];
  152.         nv.p = p;
  153.         nv.p.cut_end(i);
  154.         nv.parent = parent;
  155.         nv.ac = ac;
  156.         nv.g[p[i]] = index;
  157.         ac = p[i];
  158.         parent = n;
  159.         p.cut_begin(i + 1);
  160.         if (i == b.len()) {
  161.             nv.leaf = true;
  162.             nv.length = wlen;
  163.         } else {
  164.             id w = create_vect();
  165.             vert & wv = dict[w];
  166.             wv.p = b;
  167.             wv.p.cut_begin(i + 1);
  168.             wv.parent = n;
  169.             wv.ac = b[i];
  170.             wv.leaf = true;
  171.             wv.length = wlen;
  172.             nv.g[b[i]] = w;
  173.         }
  174.     }
  175. }
  176.  
  177. int main(int argc, char** argv) {
  178. #ifndef ONLINE_JUDGE
  179.     freopen("input.txt", "r", stdin);
  180.     freopen("output.txt", "w", stdout);
  181. #endif
  182.     scanf("%u", &N);
  183.     getchar();
  184.     wbs = 0;
  185.     dict[0].init(0);
  186.     dict[NONE].init(NONE);
  187.     for (ui i = 0; i < N; i++) {
  188.         ui _wbs = wbs;
  189.         get_word();
  190.         pstr ps(_wbs, wbs);
  191.         if (ps.len() == 0)continue;
  192.         id wlen = ps.len();
  193.         dict[0].addstr(ps, wlen);
  194.     }
  195.     for (ui i = 0; i < dinc; i++)dict[i].print();
  196.     //previous line prints the result of suffix tree building
  197.     scanf("%u", &M);
  198.     getchar();
  199.     for (ui k = 0; k < M; k++) {
  200.         get_line();
  201.         if (bfs == 0)continue;
  202.         for (ui i = 0; i < bfs; i++) {
  203.            
  204.         }
  205.     }
  206.     printf("Passed");
  207.     return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement