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;
}

最新文章

  1. web前端--边框的特征
  2. iOS时间个性化设置设置
  3. php 截取中文字符串 - ord()函数 0xa0...
  4. linux-7 man 命令
  5. MySQL 体系结构以及各种文件类型学习汇总 (转)
  6. php测试时不出现错误信息
  7. 从UIImage的矩阵变换看矩阵运算的原理
  8. 设计模式(四)&mdash;观察者模式
  9. 【HDOJ 1086】 模板水过
  10. RGB与HSV之间的转换公式及颜色表
  11. 项目Beta冲刺Day1
  12. 「Android」 Surface分析
  13. Python基础-修改excel、redis、接口开发、组织代码
  14. 【API知识】类型转换工具ConvertUtils引发的思考
  15. Git 提交的正确姿势:Commit message 编写指南
  16. bzoj4812: [Ynoi2017]由乃打扑克
  17. 矩阵乘法在numpy/matlab/数学上的不同
  18. Elasticsearch NEST – Examples for mapping between Query and C#
  19. [SCOI2016]背单词——trie树相关
  20. Linux虚拟机忘记root密码的拯救办法

热门文章

  1. SpringBoot配置文件(2)
  2. 数学建模学习笔记 | matlab基本命令及用法
  3. 【LeetCode】数组排序题 Array_Sorts
  4. 【Linux】将ens33修改为eth0 网卡方法
  5. 【Oracle】生成随机数
  6. CodeMonke少儿编程第1章 step与turn
  7. Http中的options请求
  8. python工业互联网应用实战3—Django Admin列表
  9. CSRF Cross-site request forgery 跨站请求伪造
  10. maven打包三种方式