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. Codeforces Round #309 (Div. 1)

    A. Kyoya and Colored Balls 大意: 给定$k$种颜色的球, 第$i$种颜色有$c_i$个, 一个合法的排列方案满足最后一个第$i$种球的下一个球为第$i+1$种球, 求合法方 ...

  2. 终身机器学习(Lifelong Machine Learning)综述

    终身机器学习(Lifelong Machine Learning)综述 2015年10月23日 17:34:57 qrlhl 阅读数 7805更多 分类专栏: 机器学习   版权声明:本文为博主原创文 ...

  3. 22-Perl Socket 编程

    1.Perl Socket 编程Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求,使主机间或者一台计算机上的进程间可以通讯. ...

  4. C#开发微信公众平台-就这么简单(转载)(附原文链接)

    一直使用的是一百八的诺鸡鸭,没有想去接触看起来风风火火的移动互联网:但因工作需要维护一个微信公众订阅号,考虑以前有做网站的基础,就想着做个简单的微信后台管理:看了官方的开发文档,比狗哥地图的短许多,又 ...

  5. JS基础_赋值运算符

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. O027、看nova-scheduler如何选择计算节点

    参考https://www.cnblogs.com/CloudMan6/p/5441782.html   本节重点介绍 nova-scheduler 的调度机制和实现方法:即解决如何选择在那个计算节点 ...

  7. C语言无法使用引用,一定要使用怎么办? ------指针的指针做参数

    #include <stdio.h> #include <stdlib.h> #include <string.h> void fun1(char** s); vo ...

  8. js小功能3:一个简单的计算器功能

    html: <input type='text' id='txt1' /> <select id='select'> <option value='+'>+< ...

  9. Centos7搭建Docker部署LNMP

    1.首先呢先更新yum源 yum update 2.1.安装docker存储库 yum install -y yum-utils \ device-mapper-persistent-data \ l ...

  10. 第五篇.python进阶

    目录 第五篇.python进阶 1. 异常处理 2. 数字类型内置方法 2.定义: 3.常用操作+内置方法: 4.存一个值or多个值: 5.有序or无序: 6.可变和不可变 1.用途: 2.定义: 3 ...