squee_spoon and his Cube VI(贪心,找不含一组字符串的最大长度+kmp)
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
Sample Output
#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)的更多相关文章
- squee_spoon and his Cube VI---郑大校赛(求最长子串)
市面上最常见的魔方,是三阶魔方,英文名为Rubik's Cube,以魔方的发明者鲁比克教授的名字命名.另外,二阶魔方叫Pocket Cube,它只有2*2*2个角块,通常也就比较小:四阶魔方叫Reve ...
- [Python3 练习] 010 找出藏在字符串中的“密码”
题目:找出藏在字符串中的"密码" (1) 描述 1) 题源 1 Python Challenge, level 3 2) 题源 2 小甲鱼老师的 Python 课程,第 20 讲课 ...
- 在docker容器中vi指令找不到
在使用docker容器时,有时候里边没有安装vi,敲vi命令时提示说:vi: command not found,这个时候就需要安装vi,可是当你敲apt-get install vi命令时,提示: ...
- Codeforces 870C Maximum splitting (贪心+找规律)
<题目链接> 题目大意: 给定数字n,让你将其分成合数相加的形式,问你最多能够将其分成几个合数相加. 解题分析: 因为要将其分成合数相加的个数最多,所以自然是尽可能地将其分成尽可能小的合数 ...
- hdu - 6277,2018CCPC湖南全国邀请赛B题,找规律,贪心找最优.
题意: 给出N个小时,分配这些小时去写若干份论文,若用1小时写一份论文,该论文会被引用A次,新写一篇论文的话,全面的论文会被新论文引用一次. 找最大的H,H是指存在H遍论文,而且这些论文各被引用大于H ...
- ZOJ 3829 Known Notation --贪心+找规律
题意:给出一个字符串,有两种操作: 1.插入一个数字 2.交换两个字符 问最少多少步可以把该字符串变为一个后缀表达式(操作符只有*). 解法:仔细观察,发现如果数字够的话根本不用插入,数字够的最 ...
- KMP笔记√//找最大子串,前缀自匹配长度
假设s1里找s2,然后s2进去匹配假设在第三位失配那么说明前两位是匹配成功的 如果这时候将s2后移一位相当于将s2的第一位和s2的第二位比较,如果我们已知s1(1)≠s1(2)那么就可以直接后移两位 ...
- POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20966 Accepted: 9279 Descr ...
- 【VI】如何删除匹配指定字符串的行(已解决)
命令: g/pattern/d 如,删除包含字母 hell 的行 g/hell/d 删除 不 匹配指定字符的行(未验证,有需要的朋友可以试一下) v/pattern/d g!/pattern/d
随机推荐
- [C#参考]细说进程、应用程序域与上下文之间的关系
原文转载链接:http://www.cnblogs.com/leslies2/archive/2012/03/06/2379235.html Written by:风尘浪子 引言 本文主要是介绍进程( ...
- 为什么IIS中找不到.net framework 4.5(转)
.net 4.5是4.0的update,所以直接用4.0部署就可以了 .NET 4.5 is an in-place replacement for .NET 4.0, When .NET 4.5 i ...
- codeforces 632E. Thief in a Shop fft
题目链接 E. Thief in a Shop time limit per test 5 seconds memory limit per test 512 megabytes input stan ...
- MYSQL group_concat() 函数
看来看一下表中的数据 select * from t; 下一步来看一下group_concat函数的用法 select ID,group_concat(Name) from t group by ID ...
- 强化:把treeview的QString路径转换为QModelIndex节点,有了节点就什么都好办了
http://doc.qt.io/qt-4.8/qdirmodel.html#index-2 甚至还能直接调用setData: setData(const QModelIndex &index ...
- javascript简单对象创建
由于javascript中定义了一个函数就相当于定义了一个类,我们当然可以创建一个这个类的对象. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 ...
- Android利用tcpdump和wireshark抓取网络数据包
Android利用tcpdump和wireshark抓取网络数据包 主要介绍如何利用tcpdump抓取andorid手机上网络数据请求,利用Wireshark可以清晰的查看到网络请求的各个过程包括三次 ...
- HDU1069:Monkey and Banana(DP+贪心)
Problem Description A group of researchers are designing an experiment to test the IQ of a monkey. T ...
- opengl模板缓冲区
相信大家有些人对opengl的模板缓冲区不是很理解,包括我最开始也是,opengl的模板缓冲区其实就是采用过滤的技术来控制那些颜色可以绘制,那些不能进行绘制.这里的过滤技术也就是我们的一个控制方法,主 ...
- xcode 资源管理
我个人觉得这么理解就够了 其他的以后再说