2013年1月5日

UVa 475 - Wild Thing


#include <cstdio>
#include <string>
using std::string;

int main() {
  char in[1000];
  string s, target;
  bool find = 0, first = 1;
  while (gets(in)) {
    target = string(in);
    bool star = 0;
    int len = target.length(), N = 0;
    for (int i = 0; i < len; i++)
      if (target[i] == '*') star = 1;
    string temp, compare[1000];
    for (int i = 0; ; i++)
      if (target[i] && target[i] != '*') {
        temp.insert(temp.end(), target[i]);
      } else {
        if (temp != "") compare[N++] = temp;
        temp = "";
        if (i == len) break;
      }
    find = 0;
    while (gets(in) && in[0]) {
      s = string(in);
      bool flag = 1, head = (target[0] != '*'), tail = 0;
      if (!star && s != target) flag = 0;
      else {
        int loc = 0;
        for (int i = 0; i < N && flag; i++) {
          int pos = s.find(compare[i], loc);
          if (i == N - 1 && target[len - 1] != '*') tail = 1;
          if (pos == string::npos || (!i && head && pos) || (tail && pos + compare[i].length() != s.length())) {
            flag = 0;
            if (tail)
              while (pos != string::npos && !flag) {
                if (pos + compare[i].length() == s.length()) flag = 1;
                pos = s.find(compare[i], pos + 1);
              }
          }
          loc = pos + 1;
        }
      }
      if (flag) {
        if (!find && !first) puts("");
        if (!find) printf("MATCHES FOR THE PATTERN: %s\n", target.c_str());
        find = 1;
        printf("%s\n", s.c_str());
        first = 0;
      }
    }
  }
  return 0;
}

沒有留言:

張貼留言