Bazinga

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

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个字符串,s[i] (1≤i≤n),找到最大的i,是得存在j(1≤j<i)使得,s[j]不是 s[i]的子串。

#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; char s[550][2200];
int Next[550][2200]; int Judge(int w, int o)
{
int i = 0 , j = 0;
int l1, l2;
l1 = strlen(s[w]);
l2 = strlen(s[o]); while(i < l1 && j < l2)
{
if(s[w][i] == s[o][j] || i == -1)
{
i++;
j++;
}
else
i = Next[w][i]; if(i == l1)
{
return true;
}
}
return false;
} void getNext(int q)
{
int j, k;
int i = strlen(s[q]);
j = 0;
k = -1;
Next[q][0] = -1; while(j < i)
{
if(k == -1 || s[q][j] == s[q][k])
{
j++;
k++;
Next[q][j] = k;
}
else
k = Next[q][k];
}
} int main()
{
int t, n, index, l = 1;
scanf("%d", &t); while(t--)
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%s", s[i]);
getNext(i);
}
index = -1;
int q;
for(int i = n-2; i >= 0; i--)
{
if(!Judge(i, i+1)) 找到一个字符串不满足前后是子串关系后,从后向前找最大的不满足子串的串
{
q = i;
for(int j = n-1; j > q; j--)
{
if(!Judge(q, j))
index = index > j ? index : j;
}
}
}
if(index == -1)
printf("Case #%d: %d\n", l++, index);
else
printf("Case #%d: %d\n", l++, index+1);
}
return 0;
}

  strstr阔以考虑……脉动回来

学长递归写法……

#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; int n, num;
char s[550][2200]; void slove(int k)
{
if(k == 0 || num == n-1)
return ;
if(strstr(s[k], s[k-1]))
slove(k-1); // 向下寻找
else
{
for(int i = n-1; i >= num; i--) // 找最大的 下标
{
if(strstr(s[i], s[k-1]) == 0)
{
num = i;
break; //
}
}
slove(k-1); // 继续向下寻找看是否有 存在 k 使 num 最大
}
} int main()
{
int t, l = 1;
scanf("%d", &t); while(t--)
{
scanf("%d", &n);
num = -1;
for(int i = 0; i < n; i++)
scanf("%s", s[i]);
slove(n-1);
printf("Case #%d: %d\n", l++, num == -1 ? num : num+1);
}
return 0;
}

  

Bazinga的更多相关文章

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

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

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

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

  3. hdu 5510 Bazinga(字符串kmp)

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

  4. Bazinga HDU 5510 Bazinga(双指针)

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

  5. HDU 5510:Bazinga(暴力KMP)

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

  6. TTTTTTTTTTTTTTTT hdu 5510 Bazinga 字符串+哈希

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

  7. Bazinga means

    Bazinga means Bazinga https://www.dictionary.com/e/slang/bazinga/ refs xgqfrms 2012-2020 www.cnblogs ...

  8. HDU 5510 Bazinga (字符串匹配)

    题目:传送门. 题意:t 组数据,每组 n 个串,对于第 i 个串如果存在 1 到 i-1 中的某个串不是 i 的子串,那么这个第 i 个串符合题意,求 i 的最大值. 题解:KMP,AC自动机也可以 ...

  9. [HDOJ5510]Bazinga(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 普通集合会tle,换高贵的并查集. #include <algorithm> #in ...

随机推荐

  1. 解决172.17 或者172.18 机房环境下harbor服务器不通的问题

    直接改docker-compose.yml文件: 把原来的network选项注释掉,自定义 #networks: # harbor: # external: false networks: harbo ...

  2. Scrapy输出文件格式问题汇总

    Q:Scrapy抓取的内容(包含中文)输出到JSON Lines文件时如何确保输出的是字符本身而不是其unicode编码? A:默认的JsonLinesItemExporter其ensure_asci ...

  3. 第二周总结.Java

    本学期开始学习Java课程了,首先我先说说学习Java的感觉吧,它不像C语言程序设计,但是又有语言开发的共同点.学Java语言重点是面向对象的程序设计,更加的适应生活需要和计算机开发的需要. 总的来讲 ...

  4. (3.5)常用知识-NULL与零长度、字符串尾部填充空格

    概述:NULL与零长度是不同的,NULL表示数据未知或不可用,它是与零(数值或2进制).零长度字符串不 同的一种值,也可以理解为一种状态. 即可以理解为:所有的变量都有2种状态,一种有值,一种为NUL ...

  5. JS案例经验1

    一 可以通过设置在一个div中的多个div的定位属性为absolute,从而使得这几个元素重叠.他们都脱离了标准流. 二 对于absolute的left和right属性,当left和right同时出现 ...

  6. tensorflow学习笔记一----------tensorflow安装

    2016年11月30日,tensorflow(https://www.tensorflow.org/)更新了0.12版本,这标志着我们终于可以在windows下使用tensorflow了(但是还是推荐 ...

  7. 最长上升子序列(LIS) Medium2

    JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two ...

  8. 服务性能指标:PV、UV、TPS、QPS

    名词解释 PV Page View,网页浏览量.网页被读者调用浏览的次数.网页每次打开或刷新一次页面,记录一次.用户对同一页面的多次访问,访问量累计. UV Unique Visitor,独立访问者. ...

  9. 剑指offer-重构二叉树-树-python

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  10. Site error: the ionCube PHP Loader needs to be installed.解决办法

    问题描述: 有些模块的作者为了保护代码而采用ionCube加密的代码,所以这里必须给服务器装上这个php的扩展,就好像以前的zend一样 解决办法: http://bbs.52jscn.com/thr ...