2013年1月5日

UVa 123 - Searching Quickly


#include <iostream>
#include <sstream>
#include <string>
#include <set>
using namespace std;

int main() {
  string s;
  set<string> ign, prt;
  while (cin >> s && s != "::")
    ign.insert(s);
  string input[300];
  int n = 0;
  while (getline(cin, s)) {
    for (int i = 0; s[i]; i++)
      if (s[i] >= 'A' && s[i] <= 'Z') s[i] = s[i] - 'A' + 'a';
    input[n++] = s;
    istringstream ss(s);
    while (ss >> s)
      if (!ign.count(s)) prt.insert(s);
  }
  for (set<string>::iterator it = prt.begin(); it != prt.end(); it++)
    for (int i = 0; i < n; i++) {
      int pos = 0, len = (*it).length();
      while (input[i].find(*it, pos) != string::npos) {
        pos = input[i].find(*it, pos);
        if ((!pos || input[i][pos - 1] == ' ') && (!input[i][pos + len] || input[i][pos + len] == ' ')) {
          string p = input[i], rep = *it;
          for (int j = 0; rep[j]; j++)
            rep[j] = rep[j] - 'a' + 'A';
          p.replace(p.find(*it, pos), len, rep);
          cout << p << "\n";
        }
        pos++;
      }
    }
  return 0;
}

沒有留言:

張貼留言