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

题意:给你几个字符串,让你找到最长不下降子序列,使得前一个字符串是后一个字符串的开头和结尾。

思路:用字符串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)的更多相关文章

  1. [知识点]字符串Hash

    1.前言 字符串的几大主要算法都多少提及过,现在来讲讲一个称不上什么算法, 但是非常常用的东西——字符串Hash. 2.Hash的概念 Hash更详细的概念不多说了,它的作用在于能够对复杂的状态进行简 ...

  2. 【BZOJ-3555】企鹅QQ 字符串Hash

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1545  Solved: 593[Submit][Statu ...

  3. POJ 1200 字符串HASH

    题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...

  4. LA4671 K-neighbor substrings(FFT + 字符串Hash)

    题目 Source http://acm.hust.edu.cn/vjudge/problem/19225 Description The Hamming distance between two s ...

  5. 各种字符串Hash函数比较(转)

    常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...

  6. 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

    Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...

  7. 字符串hash - POJ 3461 Oulipo

    Oulipo Problem's Link ---------------------------------------------------------------------------- M ...

  8. 长度有限制的字符串hash函数

    长度有限制的字符串hash函数 DJBHash是一种非常流行的算法,俗称"Times33"算法.Times33的算法很简单,就是不断的乘33,原型如下 hash(i) = hash ...

  9. hdu 4622 Reincarnation 字符串hash 模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...

随机推荐

  1. NodeJS各个平台安装详细

    http://www.runoob.com/nodejs/nodejs-install-setup.html 记录

  2. 大数相加Java

    题目 以字符串的形式读入两个数字,编写一个函数计算它们的和,以字符串形式返回. 分析 两个字符串,定义两个指针,分别从这两个字符串的结尾开始遍历,因为可能字符串1比字符串2长度要长,因此只要两者其中有 ...

  3. 什么是开发中经常说的'POCO'

    什么是开发中经常说的'POCO'Posted By : 蓝狐Updated On : 2015-07-19在看一些EF的文章,经常提到POCO这个词,但是,有没有比较详细的说这个POCO是什么意思呢? ...

  4. luoguP2016 战略游戏

    题目描述 Bob喜欢玩电脑游戏,特别是战略游戏.但是他经常无法找到快速玩过游戏的办法.现在他有个问题.他要建立一个古城堡,城堡中的路形成一棵树.他要在这棵树的结点上放置最少数目的士兵,使得这些士兵能了 ...

  5. 指针锁定 Pointer Lock API 用法

    指针锁定 Pointer Lock API 通过它可以访问原始的鼠标运动(基于指针的相对位移 movementX / movementY),把鼠标事件的目标锁定到一个特定的元素,同时隐藏视图中的指针光 ...

  6. 温习数据算法—js滑块验证码

    前言 大多数的应用软件都需要输入一些验证码,验证码的样式也多种多样. 比如抢票,提交订单需要验证码,很多人就纳闷了,怎么还需要验证码呢?这不是浪费时间嘛. 存在即合理,合理就是现实的. 源码下载地址+ ...

  7. U盘制作系统启动盘方法

    1.下载一个UltralSO用来把CentOS系统镜像写入U盘作为启动安装盘 U盘用一个空U盘,会格式化的. 下载下来,使用试用版就行 刻录完成.

  8. 【转载】HTTP 协议详细介绍

    背景 当你在浏览器地址栏敲入"http://www.cnblogs.com/",然后猛按回车,呈现在你面前的,将是博客园的首页了(这真是废话,你会认为这是理所当然的).作为一个开发 ...

  9. Uber三代API 生命周期管理平台实现 Uber

    Uber三代API 生命周期管理平台实现 - InfoQ https://www.infoq.cn/article/H8Ml6L7vJGQz0efpWvyJ Uber 三代 API 生命周期管理平台实 ...

  10. 微服务中台落地 中台误区 当中台遇上DDD,我们该如何设计微服务

    小结: 1. 微服务中台不是 /1堆砌技术组件就是中台 /2拥有服务治理就是中台 /3增加部分业务功能就是中台 /4Cloud Native 就是中台 https://mp.weixin.qq.com ...