[数据结构-hash]CF 7D Palindrome Degree
2016-03-10 16:01
381 查看
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\n",v); return 0; }
Hash法判断字符串是否为回文串,如果是回文串再用dp的方法统计答案!
这里主要用了hash的方法!值得学习。显然a、b、p的值早就超出int了,而且a等于b也不一定就是回文串,但是这个就是对的!!!因为这种情况产生冲突的几率很小。
Hash,一般翻译做"散列",也有直接音译为"哈希"的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值。这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,所以不可能从散列值来唯一的确定输入值。简单的说就是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。
相关文章推荐
- 数据结构—绪论
- 数据结构与算法Javascript描述(二)队列
- 【c++版数据结构】单链表复习之常见面试题型1
- 数据结构与算法Javascript描述(一)栈
- 数据结构
- nginx源码初读(11)--让烦恼从数据结构开始(ngx_command/ngx_module/ngx_conf)
- Zookeeper - 介绍篇(2)
- 单链表排序之选择排序
- 数据结构——队列
- 【数据结构课程大作业】通信录管理系统
- 【数据结构课程大作业】通信录管理系统
- Tair缓存系统学习 (数据结构存储系统)
- 数据结构 第二章 线性表(3)顺序线性表合并
- 数据结构和算法 (二)数据结构基础之树、二叉树
- 数据结构和算法 (二)数据结构基础、线性表、栈和队列、数组和字符串
- [数据结构和算法]状态树的问题
- bzoj2049[洞穴勘测]纯粹的LCT
- LeetCode 83. Remove Duplicates from Sorted List
- 数据结构——堆栈
- [补充]LCT有关细节的处理及理解