Advertisement
Malinovsky239

С (Region 2012)

Jan 21st, 2012
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. #define N 1005
  6.  
  7. using namespace std;
  8.  
  9. string temp = "<>/", s, st[N];
  10. int top;
  11.  
  12. bool check() {
  13.     top = 0;
  14.     for (int i = 0; i < s.size();) {
  15.         if (s[i] != '<')
  16.             return false;
  17.         int j = i + 1;
  18.         while (j < s.size() && s[j] != '>') j++;
  19.  
  20.         if (j > s.size())
  21.             return false;
  22.  
  23.         string sub = "";
  24.         bool close = false;
  25.         for (int k = i + 1; k < j; k++) {
  26.             if (s[k] == '/') {
  27.                 close = true;
  28.                 if (k != i + 1)
  29.                     return false;
  30.             }
  31.             else {
  32.                 sub += s[k];
  33.             }
  34.         }  
  35.         if (!sub.size())
  36.             return false;
  37.  
  38.         if (close) {
  39.             if (!top)
  40.                 return false;
  41.             if (sub != st[top - 1])                            
  42.                 return false;
  43.             top--;
  44.         }
  45.         else {
  46.             st[top++] = sub;
  47.         }          
  48.  
  49.         i = j + 1;
  50.     }  
  51.     if (!top)
  52.         return true;
  53.     return false;
  54. }
  55.  
  56. int main() {
  57.     freopen("xml.in", "r", stdin);
  58.     freopen("xml.out", "w", stdout);
  59.  
  60.     cin >> s;
  61.     for (int i = 0; i < s.size(); i++) {       
  62.         char mem = s[i];
  63.         for (char j = 'a'; j <= 'z'; j++) {
  64.             if (mem != j) {
  65.                 s[i] = j;          
  66.                 if (check()) {
  67.                     cout << s << endl;
  68.                     return 0;
  69.                 }  
  70.             }
  71.         }
  72.         for (int j = 0; j < 3; j++) {
  73.             if (mem != temp[j]) {
  74.                 s[i] = temp[j];
  75.                 if (check()) {
  76.                     cout << s << endl;
  77.                     return 0;
  78.                 }
  79.             }
  80.         }
  81.         s[i] = mem;
  82.     }
  83.  
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement