Bazinga 字符串HASH 这题不能裸HASH 要优化 毒瘤题
Don't tilt your head. I'm serious.
For nn given strings S1,S2,⋯,SnS1,S2,⋯,Sn, labelled from 11 to nn, you should find the largest i (1≤i≤n)i (1≤i≤n)such that there exists an integer j (1≤j<i)j (1≤j<i) and SjSj is not a substring of SiSi.
A substring of a string SiSi is another string that occurs in SiSi. For example, ``ruiz" is a substring of ``ruizhang", and ``rzhang" is not a substring of ``ruizhang".
InputThe first line contains an integer t (1≤t≤50)t (1≤t≤50)which is the number of test cases.
For each test case, the first line is the positive integer n (1≤n≤500)n (1≤n≤500) and in the following nn lines list are the strings S1,S2,⋯,SnS1,S2,⋯,Sn.
All strings are given in lower-case letters and strings are no longer than 20002000 letters.OutputFor each test case, output the largest label you get. If it does not exist, output −1−1.Sample Input
4
5
ab
abc
zabc
abcd
zabcd
4
you
lovinyou
aboutlovinyou
allaboutlovinyou
5
de
def
abcd
abcde
abcdef
3
a
ba
ccc
Sample Output
Case #1: 4
Case #2: -1
Case #3: 4
Case #4: 3 这题用hash预处理 然后用vector从上到下扫过去
类似于维护当前未匹配的序列
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define fuck(x) cout<<"["<<x<<"]"<<endl
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("DATA.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x7fffffff;
const int maxn = 5e4 + ;
ULL HASH[], seed = , p[], mp[][];
char s2[],a[][];
int t, n;
void init() {
p[] = ;
for (int i = ; i <= ; i++)
p[i] = p[i - ] * seed;
}
int check(int i, int j) {
int len1 = strlen(a[i]), len2 = strlen(a[j]);
for (int k = ; k <= len1 - len2 ; k++){
if (mp[i][k + len2] - mp[i][k]*p[len2] == HASH[j]) return ;
}
return ;
}
int main() {
init();
int cas=;
sf(t);
while(t--) {
sf(n);
for (int i = ; i <= n ; i++) {
scanf("%s", a[i]);
int len = strlen(a[i]);
int top = ;
mp[i][] = ;
for (int j = ; j < len ; j++) {
s2[top++] = a[i][j];
mp[i][top] = mp[i][top - ] * seed + a[i][j];
}
HASH[i] = mp[i][top];
}
int ans=-;
vector<int>s;
s.clear();
s.push_back();
for (int i= ;i<=n ;i++) {
while(s.size()) {
if ( check(i,s[s.size()-]) ) s.pop_back();
else break;
}
s.push_back(i);
if (s.size()>) ans=i;
}
printf("Case #%d: %d\n",cas++,ans);
}
return ;
}
Bazinga 字符串HASH 这题不能裸HASH 要优化 毒瘤题的更多相关文章
- hash算法和常见的hash函数 [转]
Hash,就是把任意长度的输入,通过散列算法,变换成固定长度的输出,该输出就是散列值. 这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能 会散列成相同的输出,而不 ...
- hdu 5510 Bazinga(字符串kmp)
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 使用Hash直接登录Windows(HASH传递)
抓取windows hash值 得到administrator的hash: 598DDCE2660D3193AAD3B435B51404EE:2D20D252A479F485CDF5E171D9398 ...
- 11.redis cluster的hash slot算法和一致性 hash 算法、普通hash算法的介绍
分布式寻址算法 hash 算法(大量缓存重建) 一致性 hash 算法(自动缓存迁移)+ 虚拟节点(自动负载均衡) redis cluster 的 hash slot 算法 一.hash 算法 来了一 ...
- Redis集群环境各节点无法互相发现与Hash槽分配异常 CLUSTERDOWN Hash slot not served的解决方式
总结/朱季谦 在搭建Redis5.x版本的集群环境曾出现各节点无法互相发现与Hash槽分配异常 CLUSTERDOWN Hash slot not served的情况,故而把解决方式记录下来. 在以下 ...
- 【刷题】LOJ 6227 「网络流 24 题」最长k可重线段集问题
题目描述 给定平面 \(\text{xoy}\) 上 \(n\) 个开线段组成的集合 \(\text{I}\) ,和一个正整数 \(k\) ,试设计一个算法. 从开线段集合 \(\text{I}\) ...
- 一些简单二分题,简单的hash,H(i),字符串题
说在前面: 题是乱七八糟的. 几个二分的题. (但是我的做法不一定是二分,有些裸暴力. 1. Equations HDU - 1496 输入a,b,c,d问你这个方程有多少解.a*x1^2+b*x2^ ...
- 魔咒词典 HDU - 1880 (字符串hash 单hash转int或者 双hash )
哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词 ...
- 51Nod 1277 字符串中的最大值(KMP,裸题)
1277 字符串中的最大值 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 一个字符串的前缀是指包含该字符第一个字母的连续子串,例如: ...
随机推荐
- [SHELL]linux环境变量
- 骰子涂色 (Cube painting,UVa 253)
题目描述:算法竞赛入门习题4-4 题目思路:1.旋转其中一个骰子进行匹配 2.进行遍历,如果匹配,就进行相对面的匹配 3.三个对立面都匹配即是一样等价的 //没有按照原题的输入输出 #include ...
- spark dataset join 使用方法java
dataset<Row> df1,df2,df3 //该方法可以执行成功 df3= df1.join(df2,"post_id").selectExpr("h ...
- git revert 与 git reset
Git版本回滚之 git revert 与 git reset 在使用 git 的时候,如果错误push之后,经常会回滚版本. git的回滚有两种方式: revert命令:这种方式,是用一种反向的 p ...
- 理解glance
摘要: 本节介绍 OpenStack Image 服务 Glance 的基本概念. OpenStack 由 Glance 提供 Image 服务. 理解 Image 要理解 Image Service ...
- 通过Ajax上传文件至WebApi服务器
https://stackoverflow.com/questions/43013858/ajax-post-a-file-from-a-form-with-axios var formData = ...
- 接口_requests_基于python
HTTP request python官方文档:http://cn.python-requests.org/zh_CN/latest/ 1. 环境 基于环境,需要安装requests 模块,安装方法 ...
- 《剑指Offer》题四十一~题五十
四十一.数据流中的中位数 题目:如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么中位数就是所有数值排序之后位于中间的数值.如果从数据流中读出偶数个数值,那么中位数就是所有数值排序之后中 ...
- Agri-Net(最小生成树)
Description Farmer John has been elected mayor of his town! One of his campaign promises was to brin ...
- 11.24Daily Scrum(2)
人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.996 数据库测试 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.997 实现视频浏览的功能 王 ...