zjnuSAVEZ (字符串hash)
Description
There are eight planets and one planetoid in the Solar system. It is not a well known fact that there is a secret planet S4 inhabited by small creatures similar to bears, their codename being Lodas. Although this fact is well hidden from the public, the association
Savez sent a team lead by general Henrik to study the Lodas. It has been discovered that Lodas have the ability of teleportation and he wants to hire them in his army. One Lod consists of N strings where the ith string is denoted by xi . Research has shown
that the number of teleportations a Loda can make depends on one special subsequence (not necessarily consecutive) of these strings. Strings xi and xj (i < j) can both be in that sequence if and only if string xj both starts with and ends with string xi .
The number of teleportations a Loda can make is the length of the longest described subsequence. Determine the number of teleportations.
Input
The first line of input contains of the integer N, the number of strings. Each of the following N lines contains one string consisting of uppercase letters of the English alphabet. The input data will be such that there will be less than two million characters
in total.
Output
The first and only line of output must contain the number of teleportations a Loda can make.
Sample Input
5
A
B
AA
BBB
AAA
5
A
ABA
BBB
ABABA
AAAAAB
6
A
B
A
B
A
B
Sample Output
3
3
3
题意:给你几个字符串,让你找到最长不下降子序列,使得前一个字符串是后一个字符串的开头和结尾。
思路:用字符串hash做。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define MOD 1000000007
#define maxn 2000050
#define mod1 31
#define mod2 1000000007
ll md[maxn];
map<ll,int>mp;
map<ll,int>::iterator it;
void init()
{
int i;
md[0]=1;
for(i=1;i<=2000000;i++){
md[i]=(md[i-1]*mod1)%mod2;
}
}
char s[2000060];
int main()
{
int n,m,i,j,len;
init();
while(scanf("%d",&n)!=EOF)
{
if(n==0){
printf("0\n");continue;
}
mp.clear();
int ans=1;
for(i=1;i<=n;i++){
scanf("%s",s);
len=strlen(s);
int now=0;
ll num1=0,num2=0;
for(j=0;j<len;j++){
num1=(num1+md[j]*(s[j]-'A'+1) )%mod2;
num2=(num2*mod1+(s[len-1-j]-'A'+1))%mod2; //这里是关键
if(num1==num2){
now=max(now,mp[num1]);
}
}
ll num=0;
for(j=0;j<len;j++){
num=(num+(s[j]-'A'+1)*md[j])%mod2;
}
mp[num]=max(mp[num],now+1);
ans=max(ans,now+1);
}
printf("%d\n",ans);
}
return 0;
}
zjnuSAVEZ (字符串hash)的更多相关文章
- [知识点]字符串Hash
1.前言 字符串的几大主要算法都多少提及过,现在来讲讲一个称不上什么算法, 但是非常常用的东西——字符串Hash. 2.Hash的概念 Hash更详细的概念不多说了,它的作用在于能够对复杂的状态进行简 ...
- 【BZOJ-3555】企鹅QQ 字符串Hash
3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1545 Solved: 593[Submit][Statu ...
- POJ 1200 字符串HASH
题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...
- LA4671 K-neighbor substrings(FFT + 字符串Hash)
题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...
- 各种字符串Hash函数比较(转)
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...
- 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774
Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...
- 字符串hash - POJ 3461 Oulipo
Oulipo Problem's Link ---------------------------------------------------------------------------- M ...
- 长度有限制的字符串hash函数
长度有限制的字符串hash函数 DJBHash是一种非常流行的算法,俗称"Times33"算法.Times33的算法很简单,就是不断的乘33,原型如下 hash(i) = hash ...
- hdu 4622 Reincarnation 字符串hash 模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...
随机推荐
- Tomcat配置上遇到的一些问题
Tomcat启动:在bin目录下双击startup.bat文件就行. 访问:在浏览器输入http://localhost:8080 回车访问的是自己 的界面: http://othersip:8080 ...
- 常用的N个网站建议收藏
类型网站路径学习资源及博客论坛网站 书栈网:https://www.bookstack.cn 52 download: http://www.52download.cn/wpcourse/ 菜鸟教程: ...
- Python Kafka Client 性能测试
一.前言 由于工作原因使用到了 Kafka,而现有的代码并不能满足性能需求,所以需要开发高效读写 Kafka 的工具,本文是一个 Python Kafka Client 的性能测试记录,通过本次测试, ...
- sort方法和sorted()函数
sort方法和sorted()函数的区别: 相同点:都能完成排序操作. 不同点: (1)使用sort()方法对list排序会修改list本身,不会返回新list,sort()不能对dict字典进行排序 ...
- 【Oracle】增量备份和全库备份怎么恢复数据库
1差异增量实验示例 1.1差异增量备份 为了演示增量备份的效果,我们在执行一次0级别的备份后,对数据库进行一些改变. 再执行一次1级别的差异增量备份: 执行完1级别的备份后再次对数据库进行更改: 再执 ...
- LeetCode559.N 叉树的最大深度
题目 法一.层序遍历 1 class Solution { 2 public: 3 int maxDepth(Node* root) { 4 if(root== NULL) return 0; 5 q ...
- LeetCode501.二叉搜索树中的众数
题目,本题未做出,还有很多要学习 class Solution { public: vector<int>ans; int base,count,maxCount; void update ...
- 使用gui_upload的总结
今天使用gui_upload函数将文本文件的内容读取到内表.出现了一个问题,总是程序宕掉,出项的提示是 Type conflict when calling a function module. 原来 ...
- 20V,24V转5V,20V,24V转3.3V降压芯片,IC介绍
常用的20V和24V转5V,3.3V的LDO稳压和DC-DC降压芯片: PW6206系列是一款高精度,高输入电压,低静态电流,高速,低压降线性稳压器具有高纹波抑制.输入电压高达40V,负载电流高达10 ...
- Flask源码流程分析(一)
Flask源码流程分析: 1.项目启动: 1.实例化Flask对象 1. 重要的加载项: * url_rule_class = Rule * url_map_class = Map * session ...