Ladies and gentlemen, please sit up straight. 
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 要优化 毒瘤题的更多相关文章

  1. hash算法和常见的hash函数 [转]

       Hash,就是把任意长度的输入,通过散列算法,变换成固定长度的输出,该输出就是散列值. 这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不同的输入可能 会散列成相同的输出,而不 ...

  2. hdu 5510 Bazinga(字符串kmp)

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  3. 使用Hash直接登录Windows(HASH传递)

    抓取windows hash值 得到administrator的hash: 598DDCE2660D3193AAD3B435B51404EE:2D20D252A479F485CDF5E171D9398 ...

  4. 11.redis cluster的hash slot算法和一致性 hash 算法、普通hash算法的介绍

    分布式寻址算法 hash 算法(大量缓存重建) 一致性 hash 算法(自动缓存迁移)+ 虚拟节点(自动负载均衡) redis cluster 的 hash slot 算法 一.hash 算法 来了一 ...

  5. Redis集群环境各节点无法互相发现与Hash槽分配异常 CLUSTERDOWN Hash slot not served的解决方式

    总结/朱季谦 在搭建Redis5.x版本的集群环境曾出现各节点无法互相发现与Hash槽分配异常 CLUSTERDOWN Hash slot not served的情况,故而把解决方式记录下来. 在以下 ...

  6. 【刷题】LOJ 6227 「网络流 24 题」最长k可重线段集问题

    题目描述 给定平面 \(\text{xoy}\) 上 \(n\) 个开线段组成的集合 \(\text{I}\) ,和一个正整数 \(k\) ,试设计一个算法. 从开线段集合 \(\text{I}\) ...

  7. 一些简单二分题,简单的hash,H(i),字符串题

    说在前面: 题是乱七八糟的. 几个二分的题. (但是我的做法不一定是二分,有些裸暴力. 1. Equations HDU - 1496 输入a,b,c,d问你这个方程有多少解.a*x1^2+b*x2^ ...

  8. 魔咒词典 HDU - 1880 (字符串hash 单hash转int或者 双hash )

    哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词 ...

  9. 51Nod 1277 字符串中的最大值(KMP,裸题)

    1277 字符串中的最大值 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 一个字符串的前缀是指包含该字符第一个字母的连续子串,例如: ...

随机推荐

  1. 【WXS全局对象】Number

    属性: 名称 说明 Number.MAX_VALUE 返回JS中可表示的最大的数.它的近似值为 1.7976931348623157 x 10308. Number.MIN_VALUE 返回JS中可表 ...

  2. Y460蓝牙键盘无法连接问题解决

    mac坏了,无法启动,一直没时间去修理. 近期把大学的时候用的笔记本又翻了出来,小Y,经典的“娱乐本” Y460. Y460上之前被自己各种重装系统,反复从windows到双系统,再到linux之间来 ...

  3. 150命令之线上查询及帮助命令 man hellp

    150命令之线上查询及帮助命令 man 查询命令的帮助 man + 命令 NAME        ls - list directory contents 命令+命令简单说明   SYNOPSIS   ...

  4. Python高级编程-多线程

    (一)进程线程概述: 很多同学都听说过,现代操作系统比如Mac OS X,UNIX,Linux,Windows等,都是支持“多任务”的操作系统. 什么叫“多任务”呢?简单地说,就是操作系统可以同时运行 ...

  5. Python决定一个变量时局部的,还是全局的,是在编译期

    Python中的变量名是在编译时就解析好的,换句话说,在编译时(也就是在交互控制台输入代码是或者import文件时),Python就已经决定一个变量应该是局部变量,还是全局变量.来看下面的例子: &g ...

  6. Linearization of the kernel functions in SVM(多项式模拟)

    Description SVM(Support Vector Machine)is an important classification tool, which has a wide range o ...

  7. c# throw抛出上一个异常

    catch(exception e) { throw; } 不仅抛出这次的异常,也抛出之前的异常. 用法示例:函数A调用函数B,A用到此throw时,B中发生的异常也会继承过来. catch(exce ...

  8. (一)Model的产生及处理

    MVC的概念其实最早可以追溯到很久很久以前,并不是WEB开发过程中所首创, 但是,MVC也适合WEB上的开发,并真正的在WEB开发领域广泛应用.MVC的第一个字母M是Model,承载着View层和Co ...

  9. SQLite - Python

    SQLite - Python 安装 SQLite3 可使用 sqlite3 模块与 Python 进行集成.sqlite3 模块是由 Gerhard Haring 编写的.它提供了一个与 PEP 2 ...

  10. python 爬虫 伪装

    #coding=utf-8 import requests def requests_view(response): import webbrowser requests_url = response ...