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

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;
}

  

posted @ 2015-12-10 19:04  handsomecui  阅读(334)  评论(0编辑  收藏  举报