1818: squee_spoon and his Cube VI

Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 77  Solved: 22
SubmitStatusWeb Board

Description

市面上最常见的魔方,是三阶魔方,英文名为Rubik's Cube,以魔方的发明者鲁比克教授的名字命名。另外,二阶魔方叫Pocket Cube,它只有2*2*2个角块,通常也就比较小;四阶魔方叫Revenge Cube,这是因为就算你好不容易复原了三阶魔方,四阶魔方也会向你“复仇”;而五阶魔方叫Professor Cube,人们认为只有专家才能够复原这么复杂的魔方。

作为ACM(A Cube Master),squee_spoon准备为九阶正十二面体魔方命名,此时他的脑中浮现出一个长长的字符串S,似乎可以作为魔方的英文名。但是问题没有那么简单,squee_spoon有n个不喜欢的短字符串a1~an,所以squee_spoon希望将九阶正十二面体魔方命名为S的最长子串T,在这个子串中,不能包含a1~an,即a1~an均不是T的子串。

Input

多组数据。

第一行,字符串S,长度不会超过10^5。

第二行,一个整数n,1<=n<=10。

接下来的n行,n个字符串a1~an,ai的长度不会超过10。

Output

对于每组数据,输出两个整数,分别是T的长度及其在原串S中的起始下标(下标从0开始,如果存在多解,输出最小的起始下标)。

Sample Input

orz_zzuspy 2 orz us YM_2030xxj 3 _20 03 M_

Sample Output

6 1 5 5
题解:strstr写的终于ac了。。。以前在别的oj上也写过,但是竟然ac了,数据何其之弱
strstr代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define MAX(x,y)(x>y?x:y)
const int MAXN=1000010;
char mstr[MAXN];
char str[110];
struct Node{
int s,e;
};
Node area[MAXN];
/*int cmp(const void *a,const void *b){
if((*(Node *)a).e!=(*(Node *)b).e)return (*(Node *)a).e-(*(Node *)b).e;
else return (*(Node *)a).s-(*(Node *)b).s;
}
*/
int cmp(Node a,Node b){
if(a.e!=b.e)return a.e<b.e;
else return a.s<b.s;
}
int top;
int main(){
int N;
while(~scanf("%s",mstr)){
top=0;
scanf("%d",&N);
for(int i=0;i<N;i++){
scanf("%s",str);
int len=strlen(str),c=0;
while(strstr(mstr+c,str)){
area[top].s=strstr(mstr+c,str)-mstr;
area[top].e=area[top].s+len-1;
c=area[top].s+len;
top++;
}
}
int ans=0;
int n=strlen(mstr),t=0,temp;
area[top].s=n;area[top].e=n;
//qsort(area,top+1,sizeof(area[0]),cmp);
sort(area,area+top+1,cmp);
//for(int i=0;i<=top;i++)printf("%d %d\n",area[i].s,area[i].e);
int p=0;
for(int i=0;i<=top;i++){
temp=area[i].e-t;
//ans=MAX(ans,temp);
if(ans<temp)ans=temp,p=t;
if(area[i].s+1>t)t=area[i].s+1;
}
printf("%d %d\n",ans,p);
}
return 0;
}

  kmp:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX(x,y)(x>y?x:y)
const int MAXN=1000010;
char mstr[MAXN];
char str[110];
struct Node{
int s,e;
};
Node area[MAXN];
int cmp(const void *a,const void *b){
if((*(Node *)a).e!=(*(Node *)b).e)return (*(Node *)a).e-(*(Node *)b).e;
else return (*(Node *)a).s-(*(Node *)b).s;
}
int p[110],top;
void getp(){
int i=0,j=-1;
p[0]=-1;
while(str[i]){
if(j==-1||str[i]==str[j]){
i++;j++;
p[i]=j;
}
else j=p[j];
}
}
void kmp(){
getp();
int i=0,j=0;
while(mstr[i]){
if(j==-1||mstr[i]==str[j]){
i++;j++;
if(!str[j])area[top].s=i-j,area[top++].e=i-1;
}
else j=p[j];
}
}
int main(){
int N;
while(~scanf("%s",mstr)){
top=0;
scanf("%d",&N);
for(int i=0;i<N;i++){
scanf("%s",str);
kmp();
}
int ans=0;
int n=strlen(mstr),t=0,temp;
area[top].s=n;area[top].e=n;
qsort(area,top+1,sizeof(area[0]),cmp);
//for(int i=0;i<=top;i++)printf("%d %d\n",area[i].s,area[i].e);
int p=0;
for(int i=0;i<=top;i++){
temp=area[i].e-t;
//ans=MAX(ans,temp);
if(ans<temp)ans=temp,p=t;
if(area[i].s+1>t)t=area[i].s+1;
}
printf("%d %d\n",ans,p);
}
return 0;
}

  

squee_spoon and his Cube VI(贪心,找不含一组字符串的最大长度+kmp)的更多相关文章

  1. squee_spoon and his Cube VI---郑大校赛(求最长子串)

    市面上最常见的魔方,是三阶魔方,英文名为Rubik's Cube,以魔方的发明者鲁比克教授的名字命名.另外,二阶魔方叫Pocket Cube,它只有2*2*2个角块,通常也就比较小:四阶魔方叫Reve ...

  2. [Python3 练习] 010 找出藏在字符串中的“密码”

    题目:找出藏在字符串中的"密码" (1) 描述 1) 题源 1 Python Challenge, level 3 2) 题源 2 小甲鱼老师的 Python 课程,第 20 讲课 ...

  3. 在docker容器中vi指令找不到

    在使用docker容器时,有时候里边没有安装vi,敲vi命令时提示说:vi: command not found,这个时候就需要安装vi,可是当你敲apt-get install vi命令时,提示: ...

  4. Codeforces 870C Maximum splitting (贪心+找规律)

    <题目链接> 题目大意: 给定数字n,让你将其分成合数相加的形式,问你最多能够将其分成几个合数相加. 解题分析: 因为要将其分成合数相加的个数最多,所以自然是尽可能地将其分成尽可能小的合数 ...

  5. hdu - 6277,2018CCPC湖南全国邀请赛B题,找规律,贪心找最优.

    题意: 给出N个小时,分配这些小时去写若干份论文,若用1小时写一份论文,该论文会被引用A次,新写一篇论文的话,全面的论文会被新论文引用一次. 找最大的H,H是指存在H遍论文,而且这些论文各被引用大于H ...

  6. ZOJ 3829 Known Notation --贪心+找规律

    题意:给出一个字符串,有两种操作: 1.插入一个数字  2.交换两个字符   问最少多少步可以把该字符串变为一个后缀表达式(操作符只有*). 解法:仔细观察,发现如果数字够的话根本不用插入,数字够的最 ...

  7. KMP笔记√//找最大子串,前缀自匹配长度

    假设s1里找s2,然后s2进去匹配假设在第三位失配那么说明前两位是匹配成功的 如果这时候将s2后移一位相当于将s2的第一位和s2的第二位比较,如果我们已知s1(1)≠s1(2)那么就可以直接后移两位 ...

  8. POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20966   Accepted: 9279 Descr ...

  9. 【VI】如何删除匹配指定字符串的行(已解决)

    命令: g/pattern/d 如,删除包含字母 hell 的行 g/hell/d 删除 不 匹配指定字符的行(未验证,有需要的朋友可以试一下) v/pattern/d g!/pattern/d

随机推荐

  1. oracle基础代码使用

    create or replace procedure pr_test1 is v_case ) :;--定义变量 begin -- /*判断语句 then dbms_output.put_line( ...

  2. linux install nginx error

    1 2 3 4 5 6 7 8 9 10 11 [mahao01@127.0.0.1 nginx-1.2.9]$ make make -f objs/Makefile make[1]: Enterin ...

  3. Oracle游标

    转自:http://www.cnblogs.com/fjfzhkb/archive/2007/09/12/891031.html 游标-----内存中的一块区域,存放的是select 的结果      ...

  4. hadoop搭建杂记:Linux下不同linux主机之间文件copy的scp命令

    不同的Linux之间copy文件常用有3种方法: 不同的Linux之间copy文件常用有3种方法: ①ftp 就是其中一台Linux安装ftp Server,这样可以另外一台使用ftp的程序来进行文件 ...

  5. 老旧Webkit浏览器行内元素0间距问题

    有时我们希望display:inline-block的元素之间的天衣无缝.紧密相依,比如说如下的情情形: 一般情况下我们使用如下代码可以实现: .pageNav { font-size:; text- ...

  6. JQ兼容性问题

    checkbox操作 1:设置为选中状态   $(this).prop("checked", true); 2:判断是否选中     $(this).is(":check ...

  7. no protocol specified

    基于vncserver安装oracle or oracle RAC时 以root账号运行xhost + 在切换到grid or oracle安装oracle database file or clus ...

  8. Please ensure that adb is correctly located at '...adb.exe' and can be executed.

    Android Launch! The connection to adb is down, and a severe error has occured. You must restart adb ...

  9. Error (0xc0000225) installing Windows 8 R2 on VirtualBox

    Windows Boot Manager Windows failed to start. A recent hardware or software change might be the caus ...

  10. git版本工具(团队开发常用)

    1.创建一个版本库 mkdir repository    //创建一个文件夹 git init        //把目录编程git可以管理的仓库 2.提交文件到版本库 git add test.tx ...