*[topcoder]LongWordsDiv2

http://community.topcoder.com/stat?c=problem_statement&pm=13147

此题关键在于发现ABAB的组合最多有26*26种,可以穷举,然后用判断子序列~

#include <vector>
#include <iostream>
using namespace std;

class LongWordsDiv2 {
public:
    string find(string word) {
    	for (int i = 0; i < word.size() - 1; i++) {
    		if (word[i] == word[i+1])
    			return "Dislikes";
    	}
        for (char x = 'A'; x <= 'Z'; x++) {
            for (char y = 'A'; y <= 'Z'; y++) {
                string tmp = "";
                tmp += x;
                tmp += y;
                tmp += x;
                tmp += y;
                if (subSeq(word, tmp))
                    return "Dislikes";
            }
        }
        return "Likes";
    }
    
    bool subSeq(string a, string b) {
        int i = 0;
        int j = 0;
        while (i < a.size() && j < b.size()) {
            if (a[i] == b[j])
                j++;
            i++;
        }
        return (j == b.size());
    }
};

  

posted @ 2014-08-08 14:29  阿牧遥  阅读(122)  评论(0编辑  收藏  举报