Advertisement
Guest User

Untitled

a guest
Oct 11th, 2014
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define OPEN 0
  3. #define CLOSE 1
  4. #define MAXL 1001
  5. using namespace std;
  6.  
  7. string mat[MAXL]; int N;
  8.  
  9. inline string item_tag(char t, bool closing) {
  10.     if (t != '*' && t != '#') return "";
  11.     string tag = "<";
  12.     if (closing) tag += "/";
  13.     return tag+"li>\n";
  14. }
  15.  
  16. inline string list_tag(char t, bool closing) {
  17.     if (t != '*' && t != '#') return "";
  18.     string tag = "<";
  19.     if (closing) tag += "/";
  20.     return tag+((t == '#') ? "ol" : "ul")+">\n";
  21. }
  22.  
  23. string parse(char type, int beg, int end, int pos) {
  24.     string res = "", aux;
  25.    
  26.     if (beg == end) {
  27.         res = mat[beg].substr(pos-1);
  28.         if (res != "") res +="\n";
  29.         return res;
  30.     }
  31.    
  32.     int interval_begin = -1;
  33.     char curr = '!';
  34.    
  35.     for (int i=beg; i<=end; i++) {
  36.         if (mat[i][pos] != '*' && mat[i][pos] != '#') {
  37.             if (interval_begin != -1) {
  38.                 aux = parse(curr, interval_begin, i-1, pos+1);
  39.                 if (aux != "") res += item_tag(type, OPEN) + aux + item_tag(type, CLOSE);
  40.             }
  41.            
  42.             aux = mat[i].substr(pos);
  43.             if (aux != "") res += item_tag(type, OPEN)+aux+"\n"+item_tag(type, CLOSE);
  44.            
  45.             interval_begin = -1; curr = '!';
  46.  
  47.         } else if (mat[i][pos] != curr) {
  48.             if (interval_begin != -1) {
  49.                 aux = parse(curr, interval_begin, i-1, pos+1);
  50.                 if (aux != "") res += item_tag(type, OPEN)+aux+item_tag(type, CLOSE);
  51.             }
  52.             interval_begin = i; curr = mat[i][pos];
  53.         }
  54.     }
  55.    
  56.     if (interval_begin != -1) {
  57.         aux = parse(curr, interval_begin, end, pos+1);
  58.         if (aux != "") res += item_tag(type, OPEN) + aux + item_tag(type, CLOSE);  
  59.     }
  60.    
  61.     if (res != "") res = list_tag(type, OPEN)+res+list_tag(type, CLOSE);
  62.     return res;
  63. }
  64.  
  65. int main() {
  66.     #ifdef ONLINE_JUDGE
  67.     freopen("input.txt", "r", stdin);
  68.     freopen("output.txt", "w", stdout);
  69.     #endif
  70.     int words=0;
  71.    
  72.     while (getline(cin, mat[words]))
  73.         words++;
  74.     cout << parse('!', 0, words-1, 0);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement