Bazinga

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2078    Accepted Submission(s): 642

Problem Description
Ladies and gentlemen, please sit up straight.
Don't tilt your head. I'm serious.

For n given strings S1,S2,⋯,Sn, labelled from 1 to n, you should find the largest i (1≤i≤n) such that there exists an integer j (1≤j<i) and Sj is not a substring of Si.

A substring of a string Si is another string that occurs in Si. For example, ``ruiz" is a substring of ``ruizhang", and ``rzhang" is not a substring of ``ruizhang".

 
Input
The first line contains an integer 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) and in the following n lines list are the strings S1,S2,⋯,Sn.
All strings are given in lower-case letters and strings are no longer than 2000 letters.
 
Output
For each test case, output the largest label you get. If it does not exist, output −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
 
Source
题意:,依次给出n个串, 找出下标最大且不能包含前面所有的串的串(即前面所有的串都是这个串的子串)。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f
#define FOR(i,n) for(int i=1;i<=n;i++)
#define CT continue;
#define PF printf
#define SC scanf
const int mod=1000000007;
const int N=505+10;
ull seed=13331;
ull num[N];
int a[N];
char s[505][2005]; bool inter(int id,int k)
{
int len=strlen(s[id]);
ull tmp=0,big=0,base=1; for(int i=0;i<len;i++)
{
tmp=tmp*seed+s[id][i];
big=big*seed+s[k][i];
}
for(int i=0;i<len-1;i++) base*=seed;
if(tmp==big) return true;
for(int i=len;s[k][i]!='\0';i++)
{
big=(big-s[k][i-len]*base)*seed+s[k][i];
if(big==tmp) return true;
}
return false;
} int main()
{
int cas,n,kk=0;scanf("%d",&cas);
while(cas--){
scanf("%d",&n);
int pos=0,ans=-1;
for(int i=1;i<=n;i++)
{
scanf("%s",s[i]);
while(1)
{
if(!pos) {a[++pos]=i;break;}
if(inter(a[pos],i)) pos--;
else
{
a[++pos]=i;
ans=i;
break;
}
}
}
printf("Case #%d: %d\n",++kk,ans);
}
return 0;
}

 分析:

1.复杂度没有想到合理的办法降下来,正确的做法是,设置一个栈(或数组),当栈顶字符串完全包含于当前的字符串时,

那么就弹出栈顶字符串,然后再进行比较,因此栈内保存的全是能符合题目要求的字符串:理论依据:如果a是b的子串,

那么判断时,如果b完全包含于c,那么a也必定完全包含于c,如果b不完全包含于c,那么c肯定是符合要求的字符串,弹入。

 总的复杂度:2*500*2000=2*10^6;
2.在计算base时犯了个错误,因为base是seed的(l-1)次方,所以我先循环了l次,再除以一次seed,,结果这样是错的
因为ull是64位的,超过64位自动溢出,相当于取模,所以base超了ull时,这样先循环再除肯定是错的
 

TTTTTTTTTTTTTTTT hdu 5510 Bazinga 字符串+哈希的更多相关文章

  1. hdu 5510 Bazinga(字符串kmp)

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

  2. Bazinga HDU 5510 Bazinga(双指针)

    Bazinga HDU 5510 Bazinga(双指针) 题链 解法:对于串i来说,如果串i是不符合的,那么代表串i之前的字符串都是i的子串,那么我们求一个新的i(定义为ti),如果i是ti 的子串 ...

  3. 【HDU 5510 Bazinga】字符串

    2015沈阳区域赛现场赛第2题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:给定一个由字符串组成的序列,一共n个元素,每个元素是一个不 ...

  4. HDU 5510 Bazinga 暴力匹配加剪枝

    Bazinga Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5510 ...

  5. hdu 5510 Bazinga KMP+尺取法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:至多50组数据,每组数据至多500个字符串,每个字符串的长度最长为2000.问最大的下标( ...

  6. hdu 5510 Bazinga (KMP+暴力标记)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 思路: 一开始直接用KMP莽了发,超时了,后面发现如果前面的字符串被后面的字符串包含,那么我们就 ...

  7. [HDU 4821] String (字符串哈希)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 题目大意:给你M,L两个字母,问你给定字串里不含M个长度为L的两两相同的子串有多少个? 哈希+枚 ...

  8. hdu 5510 Bazinga

    http://acm.hdu.edu.cn/showproblem.php?pid=5510 Problem Description: Ladies and gentlemen, please sit ...

  9. HDU 5510 Bazinga (2015沈阳现场赛,子串判断)

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

随机推荐

  1. Redis基本数据

    Redis Redis是一个速度非常快的非关系数据库(NoSql),它可以存储键(key)与五种不同的值(value)之间的映射.可以将存储的内存的键值对数据持久化到硬盘. Redis 数据结构 Re ...

  2. Semaphore拿到执行权的线程之间是否互斥

    java线程之间的控制,使用Semaphore 实现 互斥 下面我们通过Semaphore来实现一个比较好的互斥操作: package com.zhy.concurrency.semaphore; i ...

  3. jeesite 跳开登录页面直接访问

    把控制层的${adminPath}改为${frontPath} 访问时吧/a改为/f 也可以在jeesite配置文件内配置两个的值 http://localhost:8181/cxfvp/a?logi ...

  4. 解决IDEA报错Could not autowire. There is more than one bean of 'xxx' type

    更新项目之后IDEA突然出现了这样的报错信息.显示Could not autowire. There is more than one bean of 'xxx' type.这个错误的意思是xxx类型 ...

  5. kbmMW 5.09测试报告(1)-Scheduler

    这个版本除了增加新的SmartBinding功能,同时提供了大量的功能更新以及bug修正.其中,SmartBinding的介绍,xalion已经第一时间写了初识kbmmw 中的smartbind功能, ...

  6. JavaWeb【过滤器】

    定义: 服务器端组件,可以截取用户端的请求和响应,并对这些信息做过滤. 课程概要: 1.工作原理 2.生命周期 1.web.xml配置 注意:url-pattern配置路径前面需要加"/&q ...

  7. centos8/redhat8 无法上网,通过启动systemctl start NetworkManger搞定

    在systemd里面,可以直接使用systemctl进行管理 启动:systemctl start NetworkManger 关闭:systemctl stop NetworkManager 开机启 ...

  8. SpringMVC----视图层框架

    Spring Web模型-视图-控制器(MVC)框架是围绕DispatcherServlet设计的,DispatcherServlet将接收的请求分派给应用程序.SpringMVC具有配置处理程序映射 ...

  9. Raspberrypi 安装完MySQL之后登录不了(ERROR 1698 (28000))

    1.问题原因: 出现这是错误是因为 mysql 默认的 root 用户使用了 UNIX auth_socket_plugin 的用户认证方式,我们有下面两种方式处理问题: 修改 root 用户认证方式 ...

  10. 如何设置树莓派 -Zero 自启动连接WIFI

    1. 首先我们需要一台可以读取树莓派跟文件系统的Linux虚拟机(比如Ubuntu) 将树莓派SD卡系统插入电脑,识别并打开rootfs文件夹,切换到`rootfs/etc/wpa_supplican ...