Advertisement
Malinovsky239

Ужааас!!!

Nov 12th, 2011
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.10 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string>
  4. #include <cctype>
  5. #include <cstdlib>
  6.  
  7. #define N 1105
  8.  
  9. using namespace std;
  10.  
  11. string s, sum;
  12. int in[N][N];
  13. int y = -1, x, color = 1, mx_x, mx_y;
  14. char out[2 * N][2 * N];
  15.  
  16. void f(string tag) {
  17.     if (tag[1] == '/') {
  18.         return;
  19.     }
  20.     if (tag == "<table>")
  21.         return;
  22.     if (tag == "<tr>") {
  23.         y++;
  24.         mx_y = max(y, mx_y);
  25.         x = 0;
  26.         while (in[y][x]) {
  27.             x++;
  28.             mx_x = max(x, mx_x);
  29.         }      
  30.  
  31.         return;
  32.     }
  33.     if (tag == "<td>") {
  34.         while (in[y][x]) x++;      
  35.         in[y][x] = color++;    
  36.         x++;       
  37.         mx_x = max(x, mx_x);
  38.         return;
  39.     }
  40.     int row = 1, col = 1;
  41.  
  42.     if (tag[3] == 'c') {       
  43.         tag = tag.substr(12);
  44.         if (tag[1] == '0') {
  45.             tag = tag.substr(3);
  46.             col = 10;
  47.         }
  48.         else {
  49.             col = int(tag[0] - '0');
  50.             tag = tag.substr(2);
  51.         }
  52.  
  53.         if (tag[0] == 'r') {
  54.             tag = tag.substr(9);
  55.             if (tag[1] == '0') {
  56.                 tag = tag.substr(3);
  57.                 row = 10;
  58.             }
  59.             else {
  60.                 row = int(tag[0] - '0');
  61.                 tag = tag.substr(2);
  62.             }
  63.         }
  64.     }
  65.     else {
  66.         tag = tag.substr(12);
  67.         if (tag[1] == '0') {
  68.             row = 10;
  69.             tag = tag.substr(3);
  70.         }
  71.         else {
  72.             row = int(tag[0] - '0');
  73.             tag = tag.substr(2);
  74.         }
  75.  
  76.         if (tag[0] == 'c') {
  77.             tag = tag.substr(9);
  78.             cerr << tag << endl;
  79.             if (tag[1] == '0') {
  80.                 col = 10;
  81.                 tag = tag.substr(3);
  82.             }
  83.             else {
  84.                 col = int(tag[0] - '0');
  85.                 tag = tag.substr(2);
  86.             }
  87.         }
  88.     }
  89.     while (in[y][x]) x++;
  90.     for (int i = x; i < x + col; i++)
  91.         for (int j = y; j < y + row; j++)
  92.             if (in[j][i]) {                            
  93.                 puts("ERROR");
  94.                 exit(0);
  95.             }
  96.             else {
  97.                 in[j][i] = color;
  98.                 mx_x = max(i, mx_x);
  99.                 mx_y = max(j, mx_y);
  100.             }
  101.     color++;
  102. }
  103.  
  104. int main() {
  105.     freopen("table.in", "r", stdin);
  106.     freopen("table.out", "w", stdout);
  107.  
  108.     for (int i = 0; i < 2 * N; i++)
  109.         for (int j = 0; j < 2 * N; j++)
  110.             out[i][j] = ' ';
  111.  
  112.     while (cin >> s) {
  113.         sum += s;
  114.     }
  115.  
  116.     for (int i = 0; i < sum.size(); i++)
  117.         if (isupper(sum[i]))
  118.             sum[i] = tolower(sum[i]);
  119.  
  120.     s = "";
  121.     for (int i = 0; i < sum.size(); ) {
  122.         int pos;
  123.         s += sum[i];
  124.         if (sum[i] == '>')
  125.             for (int j = i + 1; j < sum.size(); j++)
  126.                 if (sum[j] == '<') {
  127.                     i = j - 1;
  128.                     break;
  129.                 }
  130.         i++;
  131.     }
  132.     sum = "";
  133.  
  134.     bool add = false;
  135.     for (int j = s.size() - 1; j >= 0; j--) {
  136.         if (s[j] == '>')
  137.             add = true;
  138.         if (add)
  139.             sum = s[j] + sum;
  140.     }
  141.  
  142.  
  143.     for (int i = 0; i < sum.size(); ) {
  144.         for (int j = i + 1; ; j++)
  145.             if (sum[j] == '>') {
  146.                 string tag = sum.substr(i, j - i + 1);
  147.                 f(tag);
  148.  
  149.                 i = j + 1;
  150.                 break;
  151.             }      
  152.     }
  153.  
  154.     for (int i = 0; i < N; i++) {
  155.         int j = 0, k;
  156.         while (!in[i][j]) j++;
  157.  
  158.         int w;
  159.         for (k = j; k < N; k++) {
  160.             if (!in[i][k]) {
  161.                 break;
  162.             }          
  163.             w++;
  164.         }
  165.  
  166.         j = k + 1;
  167.     }
  168.  
  169.     for (int i = 0; i < 2 * N; i++) {
  170.         for (int j = 0; j < 2 * N; j++) {
  171.             if ( (i % 2) ^ (j % 2) ) {
  172.                 if (i % 2) {
  173.                     if (j) {                       
  174.                         if (in[i / 2][j / 2] != in[i / 2][j / 2 - 1])
  175.                             out[i][j] = '|';                   
  176.                     }
  177.                     else
  178.                         if (in[i / 2][0])
  179.                             out[i][j] = '|';
  180.                 }
  181.                 if (j % 2) {
  182.                     if (i) {
  183.                         if (in[i / 2][j / 2] != in[i / 2 - 1][j / 2])
  184.                             out[i][j] = '-';
  185.                     }
  186.                     else
  187.                         if (in[0][j / 2])
  188.                             out[i][j] = '-';
  189.                 }              
  190.             }                          
  191.         }
  192.     }
  193.  
  194.     for (int i = 0; i < 2 * N; i++) {
  195.         for (int j = 0; j < 2 * N; j++) {
  196.             if (i % 2 == 0 && j % 2 == 0) {
  197.                 int s1, s2;
  198.                 if (i)
  199.                     s1 = int(out[i - 1][j] == '|') + int(out[i + 1][j] == '|');
  200.                 else
  201.                     s1 = int(out[i + 1][j] == '|');
  202.                                            
  203.                 if (j)
  204.                     s2 = int(out[i][j - 1] == '-') + int(out[i][j + 1] == '-');                
  205.                 else
  206.                     s2 = int(out[i][j + 1] == '-');
  207.  
  208.                 if (s1 * s2) {
  209.                     out[i][j] = '+';
  210.                 }
  211.                 else {
  212.                     if (s1 == 2)
  213.                         out[i][j] = '|';
  214.                     if (s2 == 2)
  215.                         out[i][j] = '-';
  216.                 }
  217.             }
  218.         }
  219.     }
  220.  
  221.     for (int i = 0; i < 2 * mx_y + 3; i++, putchar('\n')) {
  222.         int pos = 2 * N - 1;
  223.         while (out[i][pos] == ' ' && pos > 0) pos--;
  224.         for (int j = 0; j <= pos; j++) {
  225.             putchar(out[i][j]);
  226.         }
  227.     }
  228.  
  229.     return 0;
  230. }
  231.  
  232.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement