While developing Apps for Artificial Intelligence, Arush has to use specific type of strings (words) called Class C strings.
All the strings have been classified into 3 classes:
Class A : A sequence of letters that read the same forward and backward.
Class B : A string of class B can be converted to class A type by changing exactly one letter to a different letter.
For Example, CAT is class B type, since it can be changed to class A by either changing "C" to "T" or "T" to "C".
Note that "SEES" is not a class B type, since changing one letter would not make it to a class A type.
Class C : A string of class C is one that is formed by concatenating 2 strings of class B together.
For example, BATMAN is a class C type, since BAT and MAN both belong to class B type.
Given a list of words, help Arush determine which words belong to class C type and which do not.
Input: BATMAN SUPERMAN *END* Output: BATMAN YES SUPERMAN NO
http://www.codechef.com/CLCO2015/problems/CLCO07
判断一个字符串能否由两个ClassB的字符串拼成。ClassB:改一个位置刚好是回文串。这里要注意,如果一个字符串本来就是回文串,那么它是偶数的时候是不满足的,但是它是奇数的时候是满足的(可以改最中间那个,不影响)
#include <iostream> #include <map> #include <algorithm> #include <cstdio> #define ll long long #define FF(i, a, n) for (int i = a; i < n; i++) using namespace std; bool classb(string s){ //判断是不是符合ClassB int l = 0; int r = s.size() - 1; int c = 0; while (l < r) { if (s[l] != s[r]) { c++; } l++; r--; } if (c > 1) return 0; if (c == 0) { if (s.size() % 2 == 0) return 0; //分类 return 1; } return 1; } bool fun(string l, string r){ if (classb(l) && classb(r)) return 1; return 0; } int main() { while (1) { string s; cin >> s; if (s == "*END*") { break; } int f = 0; for (int i = 0; i < s.size(); i++) { //分割成两部分,枚举分割的位置 string l = ""; string r = ""; for (int j = 0; j <= i; j++) { l += s[j]; } for (int j = i + 1; j < s.size(); j++) { r += s[j]; } if (fun(l, r)) { cout << s << " YES" << endl; f = 1; break; } } if (!f) { cout << s << " NO" << endl; } } return 0; }