2013年8月10日

UVa 171 - Car Trialling

WTF..
#include <cstdio>
#include <string>
using namespace std;

bool isNnn(string & s) {
  for (int i = 0; s[i]; i++) {
    if (s[i] < '0' || s[i] > '9') {
      return false;
    }
  }
  return true;
}

bool isCas(string & s) {
  return s == "CHANGE AVERAGE SPEED" || s == "CAS";
}

bool isChange(string & s) {
  if (s.length() > 4) {
    string kmh = s.substr(s.length() - 4, 4);
    if (kmh != " KMH") {
      return false;
    }
    string s1 = s.substr(0, s.length() - 4);
    string target = " TO ";
    int start = 0;
    while (s1.find(target, start) != string::npos) {
      int pos = s1.find(target, start);
      start = pos + 1;
      string s2 = s1.substr(0, pos);
      string s3 = s1.substr(pos + target.length());
      if (isCas(s2) && isNnn(s3)) {
        return true;
      }
    }
  }
  return false;
}

bool isRecord(string & s) {
  return s == "RECORD TIME";
}

bool isTimeKep(string & s) {
  return isRecord(s) || isChange(s);
}

bool isSWord(string & s) {
  int numOfDots = 0;
  for (int i = 0; s[i]; i++) {
    if (s[i] != '.' && !(s[i] >= 'A' && s[i] <= 'Z')) {
      return false;
    }
    numOfDots += (s[i] == '.');
  }
  return s.length() && (s.length() != numOfDots);
}

bool isSignwords(string & s) {
  if (isSWord(s)) {
    return true;
  }
  string target = " ";
  int start = 0;
  while (s.find(target, start) != string::npos) {
    int pos = s.find(target, start);
    start = pos + 1;
    string s1 = s.substr(0, pos);
    string s2 = s.substr(pos + target.length());
    if (isSignwords(s1) && isSWord(s2)) {
      return true;
    }
  }
  return false;
}

bool isSign(string & s) {
  if (s.length() > 1 && (s[0] == '\"') && (s[s.length() - 1] == '\"')) {
    string s1 = s.substr(1, s.length() - 2);
    return isSignwords(s1);
  }
  return false;
}

bool isWhen(string & s) {
  return s == "FIRST" || s == "SECOND" || s == "THIRD"; 
}

bool isDirection(string & s) {
  return s == "RIGHT" || s == "LEFT"; 
}

bool isHow(string & s) {
  if (s == "GO" || s == "KEEP") { 
    return true;
  }
  string target = " ";
  int start = 0;
  while (s.find(target, start) != string::npos) {
    int pos = s.find(target, start);
    start = pos + 1;
    string s1 = s.substr(0, pos);
    string s2 = s.substr(pos + target.length());
    if (s1 == "GO" && isWhen(s2)) {
      return true;
    }
  }
  return false;
}

bool isDir(string & s) {
  string target = " ";
  int start = 0;
  while (s.find(target, start) != string::npos) {
    int pos = s.find(target, start);
    start = pos + 1;
    string s1 = s.substr(0, pos);
    string s2 = s.substr(pos + target.length());
    if (isHow(s1) && isDirection(s2)) {
      return true;
    }
  }
  target = " AT ";
  start = 0;
  while (s.find(target, start) != string::npos) {
    int pos = s.find(target, start);
    start = pos + 1;
    string s1 = s.substr(0, pos);
    string s2 = s.substr(pos + target.length());
    if (isDir(s1) && isSign(s2)) {
      return true;
    }
  }
  return false;
}

bool isNav(string & s) {
  if (isDir(s)) {
    return true;
  }
  string target = " AND THEN ";
  int start = 0;
  while (s.find(target, start) != string::npos) {
    int pos = s.find(target, start);
    start = pos + 1;
    string s1 = s.substr(0, pos);
    string s2 = s.substr(pos + target.length());
    if (isNav(s1) && isDir(s2)) {
      return true;
    }
  }
  return false;
}

bool isIns(string & s) {
  if (isNav(s) || isTimeKep(s)) {
    return true;
  }
  string target = " AND ";
  int start = 0;
  while (s.find(target, start) != string::npos) {
    int pos = s.find(target, start);
    start = pos + 1;
    string s1 = s.substr(0, pos);
    string s2 = s.substr(pos + target.length());
    if (isNav(s1) && isTimeKep(s2)) {
      return true;
    }
  }
  return false;
}

void fix(string & s) {
  for (int i = 0; s[i]; i++) {
    if (s[i] == ' ') {
      s.erase(s.begin() + i--);
    } else {
      break;
    }
  }
  for (int i = s.length() - 1; i >= 0; i--) {
    if (s[i] == ' ') {
      s.erase(s.begin() + i--);
    } else {
      break;
    }
  }
  for (int i = 1; s[i]; i++) {
    if (s[i] == ' ' && s[i - 1] == ' ') {
      s.erase(s.begin() + i--);
    }
  }
}

int main() {
  char temp[999];
  int C = 1;
  while (gets(temp)) {
    string s(temp);
    if (s == "#") {
      break;
    }
    fix(s);
    printf("%3d. %s\n", C++, isIns(s) ? s.c_str() : "Trap!");
  }
  return 0;
}

沒有留言:

張貼留言