Advertisement
cmiN

tlcs2

Apr 24th, 2011
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <ctime>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. const int N = 1024, A = 26;
  9. int lenx, leny, gmlen, * length;
  10. long ipos, maxmem;
  11. char vecx[N], vecy[N];
  12. vector<pair<int, int> > apar;
  13.  
  14. inline void read()
  15. {
  16.     scanf("%d %s", &lenx, vecx + 1);
  17.     scanf("%d %s", &leny, vecy + 1);
  18. }
  19.  
  20. void futere(long param)
  21. {
  22.     if (param >= 0) {
  23.         if ((length[param] == gmlen) && (apar[ipos].second > apar[param].second)) {
  24.             gmlen--;
  25.             futere(param - 1);
  26.             printf("%c %d %d\n", vecy[apar[param].second], apar[param].first, apar[param].second);
  27.         } else {
  28.             futere(param - 1);
  29.         }
  30.     }
  31. }
  32.  
  33. inline void write(int cnt, bool flag)
  34. {
  35.     if (flag) {
  36.         printf("case %d Y\n", cnt);
  37.         printf("%d\n", gmlen);
  38.         gmlen--;
  39.         futere(ipos - 1);
  40.         printf("%c %d %d\n", vecy[apar[ipos].second], apar[ipos].first, apar[ipos].second);
  41.     } else {
  42.         printf("case %d N\n", cnt);
  43.     }
  44. }
  45.  
  46. inline void generate()
  47. {
  48.     bool viz[A];
  49.     vector<pair<int, int> > lists[A];
  50.     memset(viz, 0, A);
  51.     apar.clear();
  52.     for (int i = 1; i <= lenx; ++i) {
  53.         short int poz = vecx[i] - 'a';
  54.         if (!viz[poz]) {
  55.             viz[poz] = 1;
  56.             for (int j = 1; j <= leny; ++j) {
  57.                 if (vecx[i] == vecy[j]) {
  58.                     lists[poz].push_back(pair<int, int>(i, j));
  59.                 }
  60.             }
  61.         }
  62.         vector<pair<int, int> >::reverse_iterator fit = lists[poz].rbegin(), lit = lists[poz].rend();
  63.         for (; fit != lit; ++fit) {
  64.             fit->first = i;
  65.             apar.push_back(*fit);
  66.         }
  67.     }
  68. }
  69.  
  70. inline void lis(int cnt)
  71. {
  72.     bool flag = 0;
  73.     long dim, i, j;
  74.     int mlen;
  75.     gmlen = 1;
  76.     ipos = 0;
  77.     dim = (long) apar.size();
  78.     if (dim) {
  79.         flag = 1;
  80.         if (dim > maxmem) {
  81.             maxmem = dim;
  82.             length = (int*) realloc(length, maxmem * sizeof(int));
  83.         }
  84.         length[0] = 1;
  85.         for (i = 1; i < dim; ++i) {
  86.             mlen = 0;
  87.             for (j = i - 1; j >= 0; --j) {
  88.                 if ((apar[i].second > apar[j].second) && (length[j] > mlen)) {
  89.                     mlen = length[j];
  90.                 }
  91.             }
  92.             length[i] = mlen + 1;
  93.             if (length[i] > gmlen) {
  94.                 gmlen = length[i];
  95.                 ipos = i;
  96.             }
  97.         }
  98.     }
  99.     write(cnt, flag);
  100. }
  101.  
  102. int main()
  103. {
  104.     //freopen("_tlcs.in", "rt", stdin);
  105.     clock_t tstart = clock();
  106.     int nr, i;
  107.     scanf("%d", &nr);
  108.     for (i = 1; i <= nr; ++i) {
  109.         if ((((float) (clock() - tstart)) / ((float) CLOCKS_PER_SEC)) < 4.8) {
  110.             read();
  111.             generate();
  112.             lis(i);
  113.         } else {
  114.             write(i, 0);
  115.         }
  116.     }
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement