2013年5月31日

UVa 245 - Uncompress

#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <set>
using namespace std;

int main() {
  char tmp[9999];
  string s;
  int first = 1;
  while (gets(tmp) && string(tmp) != "0") {
    if (!first) s.append("\n");
    s.append(tmp);
    first = 0;
  }
  vector<string> words;
  set<string> dic;
  string temp;
  for (int i = 0, j; s[i]; i++)
    if ('0' <= s[i] && s[i] <= '9') {
      int num = 0;
      while (isdigit(s[i])) {
        num = num * 10 + (s[i] - '0');
        i++;
      }
      i--;
      temp = words[words.size() - num];
      words.erase(words.begin() + words.size() - num);
      words.push_back(temp);
      printf("%s", temp.c_str());
      temp = "";
    } else {
      putchar(s[i]);
      if (isalpha(s[i])) {
        temp.insert(temp.end(), s[i]);
      } else if (temp.length()) {
        if (!dic.count(temp)) {
          dic.insert(temp);
        } else {
          for (j = 0; j < words.size(); j++)
            if (words[j] == temp) break;
          words.erase(words.begin() + j);
        }
        words.push_back(temp);
        temp = "";
      }
    }
  puts("");
  return 0;
}

沒有留言:

張貼留言