Advertisement
Guest User

Untitled

a guest
Jan 21st, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <set>
  5. #include <vector>
  6. #include <string>
  7. #include <stack>
  8.  
  9. using namespace std;
  10. #define pb push_back
  11. #define mp pake_pair
  12.  
  13. struct tag
  14. {
  15.     string ss;
  16.     bool open;//1 - da, 0 - net
  17.     tag(){}
  18.     tag(string _ss, bool _open)
  19.     {
  20.         ss = _ss;
  21.         open = _open;
  22.     }
  23. };
  24. bool chek(string st)
  25. {
  26.     vector<tag>v;
  27.     stack<tag>tags;
  28.     if (st[0] != '<') {return 0;}
  29.     for (int i = 0; i < st.size(); i++)
  30.     {
  31.         if (st[i] == '<')
  32.         {
  33.             int k = i+1;
  34.             bool zakr = 0;
  35.             if (st[k] == '/')
  36.             {
  37.                 zakr = 1;
  38.                 k++;
  39.             }
  40.             else
  41.                 if (st[k] <= 'a' && st[k] >= 'z')
  42.                     {return 0;}
  43.             int kollet = 0;
  44.             string tmp = "";
  45.             while (st[k] >= 'a' && st[k] <= 'z')
  46.             {
  47.                 kollet++;
  48.                 tmp += st[k];
  49.                 k++;
  50.             }
  51.             if (kollet == 0)
  52.                 { return 0;}
  53.             if (st[k] != '>') {return 0;}
  54.             if (k+1 < st.size() && st[k + 1] != '<') {return 0;}
  55.             i = k;
  56.             if (zakr)
  57.                 v.pb(tag(tmp,0));
  58.             else
  59.                 v.pb(tag(tmp,1));
  60.         }
  61.     }
  62.     for (int i = 0; i < v.size(); i++)
  63.     {
  64.         if (v[i].open)
  65.             tags.push(v[i]);
  66.         else
  67.         {
  68.             if (tags.empty()){return 0;}
  69.             if (v[i].ss == tags.top().ss)
  70.                 tags.pop();
  71.             else
  72.                 {return 0;}
  73.         }
  74.     }
  75.     if (!tags.empty()){return 0;}
  76.     return 1;
  77. }
  78.  
  79. int main()
  80. {
  81.     freopen("xml.in", "r", stdin);
  82.     freopen("xml.out", "w", stdout);
  83.     string stri;
  84.     cin >> stri;
  85.     string s = stri;
  86.     int i = 0, j = 0;
  87.     while (i < stri.size())
  88.     {
  89.         s = stri;
  90.         for (j = 0; j < 26; j++){
  91.         s[i] =char(j+'a');
  92.         if (chek(s) == 1)
  93.         {
  94.             cout << s;
  95.             return 0;
  96.         }
  97.         }
  98.         s[i] = '<';
  99.         if (chek(s))
  100.         {
  101.             cout << s;
  102.             return 0;
  103.         }
  104.         s[i] = '>';
  105.         if (chek(s))
  106.         {
  107.             cout << s;
  108.             return 0;
  109.         }
  110.         s[i] = '/';
  111.         if (chek(s))
  112.         {
  113.             cout << s;
  114.             return 0;
  115.         }
  116.         i++;
  117.     }
  118.     /*
  119.    
  120.    
  121.    
  122.     for (int i = 0; i < st.size(); i++)
  123.     {
  124.         if (st[i] == '<')
  125.         {
  126.             bool f = 1;
  127.             int k = i+1;
  128.             if (st[i+1] == '/')
  129.             {
  130.                 f = 0;
  131.                 k++;
  132.             }
  133.             string tmp = "";
  134.             while(st[k] >= 'a' && st[k] <= 'z')
  135.             {
  136.                 tmp += st[k];
  137.                 k++;
  138.             }
  139.             if (f)
  140.                 v.pb(tag(tmp,1));
  141.             else
  142.                 v.pb(tag(tmp,0));
  143.             i = k;
  144.         }
  145.     }
  146.     bool yes = 0;
  147.     tag oshibka, totrue;
  148.     for (int i = 0; i < v.size(); i++)
  149.     {
  150.         if (v[i].open)
  151.             tags.push(v[i]);
  152.         else
  153.         {
  154.             if (v[i].ss == tags.top().ss)
  155.                 tags.pop();
  156.             else
  157.             {
  158.                 oshibka = v[i];
  159.                 totrue = tags.top();
  160.                 totrue.open = 0;
  161.                 yes=1;
  162.                 break;
  163.             }
  164.         }
  165.     }
  166.  
  167.     for (int i = 0; i < v.size(); i++)
  168.         if (v[i].ss != oshibka.ss || v[i].open != oshibka.open)
  169.         {
  170.             if (v[i].open)
  171.                 cout << '<' << v[i].ss << '>';
  172.             else
  173.                 cout << "</"<<v[i].ss<<">";
  174.         }
  175.         else
  176.         {
  177.             if (totrue.open)
  178.                 cout << '<' << totrue.ss << '>';
  179.             else
  180.                 cout << "</"<<totrue.ss<<">";
  181.         }*/
  182.     return 0;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement