H.High String
原题:
Description:
We define the srting which can satisfy the following conditions as High String:
1)The string only contains six kinds of characters: A,B,C,D,E and F;
2)cont(A) = count(b), count(c) = cont(D),and count(E) = count(F). count(X) means the number of character X in the string;
3) Except the string itself, there won't be any substrings can satisfy the second condition.
1<=length(S)<=100000
Sample Input:
ACBEDF
ACBD
GABCD
ACBBD
Sample Output:
YES
YES
NO
NO
类型:
字符串hash
题意:
判断字符串是否满足下面三个条件:
1)只包含ABCDEF六个字符
2)A的数量=B的数量,C的数量=D的数量,E的数量=F的数量
3)除了自身,任何子串不满足条件二
思路:
设i-j子串能满足条件二,则有
count[j]['A'] - count[i]['A'] = count[j]['B'] - count[i]['B'];
交换得
count[j]['A'] - count[j]['B'] = count[i]['A'] -count[i]['B'];
这样,只要求出之前的AB,CD,EF数量差,然后用hash查找当前的差是否出现过,这样复杂度就为O(n);
附代码(未AC验证):
#include <cstdio> #include <vector> #include <cstring> using namespace std; struct node { int ab,cd,ef; }; vector <node> p[1000003]; char str[100100]; int hash(int a, int b, int c) { int seed = 13131; int ans = 1; ans *= a; ans *= seed; ans *= b; ans *= seed; ans *= c; return ans%1000003; } int main() { while (scanf("%s", str) != EOF) { int i; int len = strlen(str); int num[6] = {0}; int succeed = 1; for (i = 0; i <= 1000003; i++) { p[i].clear(); } for (i = 0; i < len && succeed; i++) { if (str[i] > 'F') { succeed = 0; break; } num[str[i]-'A']++; int ab = num[0]-num[1]; int cd = num[2]-num[3]; int ef = num[4]-num[5]; int hashn = hash(ab, cd, ef); int size = p[hashn].size(); int j; for (j = 0; j < size; j++) { node &pt = p[hashn][j]; if (pt.ab == ab && pt.cd == cd && pt.ef == ef) { succeed = 0; break; } } node t; t.ab=ab, t.cd=cd, t.ef=ef; p[hashn].push_back(t); //printf("p[%d].size() = %d\n", hashn, p[hashn].size()); } if (num[0] != num[1] || num[2] != num[3] || num[4] != num[5]) { succeed = 0; } if (!succeed) puts("NO"); else puts("YES"); } return 0; }
posted on 2013-05-19 22:33 ShineCheng 阅读(223) 评论(0) 编辑 收藏 举报