zoukankan      html  css  js  c++  java
  • 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;
    }
    View Code
  • 相关阅读:
    STL中的distance和advance的简单用法
    Excel 根据数据 快捷生成sql语句
    vi | vim 用法
    常用windows 命令
    .NETCore3.0 + EFCore中使用Oracle报“ORA-12154: TNS:could not resolve the connect identifier specified"的错误处理
    CentOS虚拟机上安装java
    Eclipse快捷键
    Spring学习笔记
    ifconfig: command not found(CentOS专版,其他的可以参考)
    利用正则表达式截取带有嵌套方括号中最内层的字符串, 无论嵌套多少层始终要最里面的方括号的内容
  • 原文地址:https://www.cnblogs.com/shinecheng/p/3087748.html
Copyright © 2011-2022 走看看