Bazinga

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

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
 
  • 找到最大ID字符串i使得存在j<i的字符串不是i的子串
  • 首先考虑如果从l到r串都满足都是r的子串,那么k>r串只需要检测r串即可
  • 如果自l串开始到l+k串都是存在小于l的串不是当前串子串,那么对于l+k+1串就要检测l-1串和l到l+k串
  • 算法出来了,就是记录当前最大id使得此id之前全是id串的子串,然后检测串的范围就是这个id到当前ID的前一位
  • 如果用这样的算法,用c的strstr即可,用kmp可以减少一半时间
 #include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e5 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; int T, n, use[maxn], mx, ans;
char s[][];
std::vector<int> v;
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%d",&T)){
for(int kase=;kase<=T;kase++){
ans=-;
scanf("%d",&n);
mx=;
use[]=;
v.clear();
scanf("%s",s[]);
for(int i=;i<=n;i++){
scanf("%s",s[i]);
int flag=;
if(strstr(s[i],s[mx])==NULL)
flag=;
if(flag&&use[i-]){
for(int j=v.size()-;j>= && flag;j--){
if(strstr(s[i],s[v[j]])==NULL)
flag=;
}
}
if(flag){
mx=i; use[i]=; v.clear();
}else{
ans=i; use[i]=; v.push_back(i);
}
} printf("Case #%d: %d\n",kase,ans);
}
}
return ;
}

HDU_5510_Bazinga的更多相关文章

随机推荐

  1. PL/SQL developer连接oracle出现“ORA-12154:TNS:could not resolve the connect identifier specified”问题的解决

    转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/50728536 本文出自[我是干勾鱼的博客] 使用PL/SQL developer ...

  2. CXSprite.cpp文件

    #include "XSprite.h" CXSprite::CXSprite(void) { m_strPic.clear(); } CXSprite::~CXSprite() ...

  3. matlab保存画框图像去白边

    在matlab图像处理中,为了标识出图像的目标区域来,需要利用plot函数或者rectangle函数,这样标识目标后,就保存图像. 一般saves保存的图像存在白边,可以采用imwrite对图像进行保 ...

  4. dp之多重背包poj2392

    题意:有k种石头,高为hi,在不超过ai的高度下,这种石头可以放置,有ci种这个石头,求这些石头所能放置的最高高度......... 思路:以往的什么硬币种数,最大硬币数之类的,他们的硬币都已经是排好 ...

  5. Centos7+PHP5.6+MySQL5.7+Zabbix4.0部署

    Centos7+PHP5.6+MySQL5.7+Zabbix4.0部署 系统版本:CentOS Linux release 7.4.1708 (Core) 最小化安装 内核版本:3.10.0-693. ...

  6. 跟着百度学PHP[8]-setcookie的其他参数学习

    setcookie(name,value,time,path,domain,secure) 第四个参数是cookie的有效路径.顾名思义就是在那个路径下是有效的.当然了其子目录下也是有效的.比如 /h ...

  7. jrtplib编译指南

    The library offers support for the Real-time Transport Protocol (RTP), The library uses the JThread  ...

  8. _BV()

    #define _BV(bit) (1 << (bit)) _BV()是把1左移N位的函数._BV(7)相当于(1<<7) 常用于位的置位或清零 示例解析: PC7=7; PO ...

  9. jquery库实现iframe自适应内容高度和宽度

    javascript原生和jquery库实现iframe自适应内容高度和宽度---推荐使用jQuery的代码! ‍<iframe src="index.php" id=&qu ...

  10. java---springMVC与strutsMVC的区别

    spring mvc与struts的区别 标签: strutsspringmvcservletactiontomcat 2011-11-24 17:34 24205人阅读 评论(6) 收藏 举报  分 ...