zoukankan      html  css  js  c++  java
  • [数据结构-hash]CF 7D Palindrome Degree

    D. Palindrome Degree
    
    time limit per test   1 second
    
    String s of length n is called k-palindrome, if it is a palindrome(回文) itself, and its prefix and suffix of length   are (k-1)-palindromes. By definition, any string (even empty) is 0-palindrome.
    
    Let's call the palindrome degree of string s such a maximum number k, for which s is k-palindrome. For example, "abaaba" has degree equals to 3.
    
    You are given a string. Your task is to find the sum of the palindrome degrees of all its prefixes.
    
    Input
    
    The first line of the input data contains a non-empty string, consisting of Latin letters and digits. The length of the string does not exceed5•106. The string is case-sensitive.
    
    Output
    
    Output the only number — the sum of the polindrome degrees of all the string's prefixes.
    
    Sample test(s)
    
    input
    
    a2A
    
    output
    
    1
    
    input
    
    abacaba
    
    output
    
    6
    #include <cstdio> 
    char s[5005000]; 
    int h[5005000]; 
    const 
    int M=3; 
       
    int main() 
    { 
        scanf("%s",s); 
        int a=0,b=0,p=1,v=0; 
        for(int i=0; s[i]; ++i) 
        { 
            a=a*M+s[i],b+=s[i]*p,p*=M; 
            if(a==b) v+=(h[i+1]=h[(i+1)/2]+1); 
        } 
        printf("%d
    ",v); 
        return 0; 
    }

      

    Hash法判断字符串是否为回文串,如果是回文串再用dp的方法统计答案!

    这里主要用了hash的方法!值得学习。显然a、b、p的值早就超出int了,而且a等于b也不一定就是回文串,但是这个就是对的!!!因为这种情况产生冲突的几率很小。

      

    Hash,一般翻译做"散列",也有直接音译为"哈希"的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值。这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,所以不可能从散列值来唯一的确定输入值。简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。

     

  • 相关阅读:
    成熟失去快乐
    明天出发
    不知其期
    不是自己的机子就是不好
    都收拾完了
    文盲
    郁闷
    好像感冒了
    又都疯了
    流水账,从我开始接触计算机时写起
  • 原文地址:https://www.cnblogs.com/lastone/p/5262271.html
Copyright © 2011-2022 走看看