Advertisement
Guest User

Untitled

a guest
Jan 21st, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 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.             if (st[st.size()-1] != '>') {return 0;}
  30.             for (int i = 0; i < st.size()-1; i++)
  31.             {
  32.                     if (st[i] == '<')
  33.                     {
  34.                             int k = i+1;
  35.                             bool zakr = 0;
  36.                             if (st[k] == '/')
  37.                             {
  38.                                     zakr = 1;
  39.                                     k++;
  40.                             }
  41.                             else
  42.                                     if (st[k] <= 'a' && st[k] >= 'z')
  43.                                             {return 0;}
  44.                             int kollet = 0;
  45.                             string tmp = "";
  46.                             while (st[k] >= 'a' && st[k] <= 'z' && k < st.size())
  47.                             {
  48.                                     kollet++;
  49.                                     tmp += st[k];
  50.                                     k++;
  51.                             }
  52.                             if (kollet == 0)
  53.                                     { return 0;}
  54.                             if (st[k] != '>') {return 0;}
  55.                             if (k+1 < st.size() && st[k + 1] != '<') {return 0;}
  56.                             i = k;
  57.                             if (zakr)
  58.                                     v.pb(tag(tmp,0));
  59.                             else
  60.                                     v.pb(tag(tmp,1));
  61.                     }
  62.             }
  63.             for (int i = 0; i < v.size(); i++)
  64.             {
  65.                     if (v[i].open)
  66.                             tags.push(v[i]);
  67.                     else
  68.                     {
  69.                             if (tags.empty()){return 0;}
  70.                             if (v[i].ss == tags.top().ss)
  71.                                     tags.pop();
  72.                             else
  73.                                     {return 0;}
  74.                     }
  75.             }
  76.             if (!tags.empty()){return 0;}
  77.             return 1;
  78.     }
  79.      
  80.     int main()
  81.     {
  82.             freopen("xml.in", "r", stdin);
  83.             freopen("xml.out", "w", stdout);
  84.             string stri;
  85.             cin >> stri;
  86.             string s = stri;
  87.             int i = 0, j = 0;
  88.             while (i < stri.size())
  89.             {
  90.                     s = stri;
  91.                     for (j = 0; j < 26; j++){
  92.                     s[i] =char(j+'a');
  93.                     if (chek(s) == 1)
  94.                     {
  95.                             cout << s;
  96.                             return 0;
  97.                     }
  98.                     }
  99.                     s[i] = '<';
  100.                     if (chek(s))
  101.                     {
  102.                             cout << s;
  103.                             return 0;
  104.                     }
  105.                     s[i] = '>';
  106.                     if (chek(s))
  107.                     {
  108.                             cout << s;
  109.                             return 0;
  110.                     }
  111.                     s[i] = '/';
  112.                     if (chek(s))
  113.                     {
  114.                             cout << s;
  115.                             return 0;
  116.                     }
  117.                     i++;
  118.             }
  119.             return 0;
  120.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement