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次的区间查询,输出每次查询区间中不同 ...
随机推荐
- Flutter 基础组件:输入框和表单
前言 Material组件库中提供了输入框组件TextField和表单组件Form. 输入框TextField 接口描述 const TextField({ Key key, // 编辑框的控制器,通 ...
- Java内存模型与线程(一)
Java内存模型与线程 TPS:衡量一个服务性能的标准,每秒事务处理的总数,表示一秒内服务端平均能够响应的总数,TPS又和并发能力密切相关. 在聊JMM(Java内存模型)之前,先说一下Java为什么 ...
- (一)React Ant Design Pro + .Net5 WebApi:先搞定服务器,顺手装个Nginx
腾讯云搞定服务器,具体过程就不赘述了,文档都有,咨询客服或者自行百度,体验一下过程. 一. 服务器 1. 云服务器 cvm 1核2G centos8.0 2. 域名注册 www.homejok.com ...
- MySQL常用的一些(就几个)聚合函数
聚合函数 (常用) 函数名称 描述 CONUT() 记数 SUM() 求和 AVG() 平均值 MAX() 最大值 MIN() 最小值 -- ================= 聚合函数 ====== ...
- ctfhub技能树—web前置技能—http协议—302跳转
开启靶机 打开环境,查看显示 点击Give me Flag后发生跳转 根据题目提示为HTTP临时重定向 简单记录一下HTTP临时重定向是什么 HTTP重定向:服务器无法处理浏览器发送过来的请求(req ...
- 快速查询表中的NULL数据
正常情况下,NULL值是不会放入B-TREE索引的,因此根据IS NULL查询的时候走的通常是全表扫描,如果记录比较少还好,记录比较多,查询会非常耗时 可以通过创建一个索引来解决 CREATE IND ...
- python--or 和 and 表达式
or表达式: 两边为一真一假,返回真: 两边都为假,返回右边: 两边都为真,返回左边: and表达式: 两边为一真一假,返回假: 两边都为假,返回左边: 两边都为真,返回右边:
- 二. SpringCloud基本Rest微服务工程搭建
1. 父工程构建 1.1 Maven项目搭建 环境 版本 JDK 1.8 Maven 3.6+ Maven模板 maven-archetype-size 删除父工程src文件 1.2 父工程pom文件 ...
- QUIC协议分析-基于quic-go
quic协议分析 QUIC是由谷歌设计的一种基于UDP的传输层网络协议,并且已经成为IETF草案.HTTP/3就是基于QUIC协议的.QUIC只是一个协议,可以通过多种方法来实现,目前常见的实现有Go ...
- babel : 无法加载文件 C:\Users\win\AppData\Roaming\npm\babel.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/ go.micros
babel报错:babel : 无法加载文件 C:\Users\win\AppData\Roaming\npm\babel.ps1,因为在此系统上禁止运行脚本.有关详细信息,请参阅 https:/ g ...