BNU 34990 Justice String 2014 ACM-ICPC Beijing Invitational Programming Contest

题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34990


DEBUG了很久,还是legal的判断函数写错了...

此题做法,枚举String1的起始位置,对string2的长度进行二分,求出最长公共前缀,然后跳过一个不匹配的地方,然后继续二分匹配,再去掉一个不匹配的地方

//700-800MS   对于hash而言已经算比较快了

下面的是自己重新写的:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
using namespace std;

#define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdin)

const ull B=31;
const int MAXN = 100000+100;
char a[MAXN],b[MAXN];
ull ah[MAXN],bh[MAXN],base[MAXN];

int n,m;

int Find(int i, int j)
{
    int up=m+1-j,down=0,mid;//二分的是长度////
    ull tmpa,tmpb;
    while(up>down+1)
    {
        mid=(up+down)/2;
        tmpa=(i==0)?ah[i+mid-1]:ah[i+mid-1]-ah[i-1]*base[mid];///
        tmpb=(j==0)?bh[j+mid-1]:bh[j+mid-1]-bh[j-1]*base[mid];///
        if(tmpa == tmpb)down=mid;
        else up=mid;
    }
    return down;
}

int legal(int st)
{
    int prelen=0,j=0,use=0;
    for(int i=st;;)
    {
        prelen=Find(i,j);
        i+=prelen+1;
        j+=prelen+1;
        use++;
        if(j>=m)return 1;
        if(use == 2)
        {
            if(j>=m)return 1;
            if(j+Find(i,j)>=m)return 1;
            return 0;
        }
        if(i>=n && j<m)return 0;
    }
}

int solve()
{
    ah[0]=a[0],bh[0]=b[0],a[n+1]=0,b[m+1]=0;
    for(int i=1;i<=m;i++)
        bh[i]=bh[i-1]*B+b[i];
    for(int i=1;i<=n;i++)
        ah[i]=ah[i-1]*B+a[i];
    for(int i=0;i<=n-m;i++)
    {
        if(legal(i))return i;
    }
    return -1;
}

int main()
{
    //IN("BNUhash.txt");
    int ncase;
    scanf("%d",&ncase);
    base[0]=1;
    rep(i,1,MAXN)
        base[i]=base[i-1]*B;
    for(int ic=1;ic<=ncase;ic++)
    {
        scanf("%s%s",a,b);
        n=strlen(a);
        m=strlen(b);
        printf("Case #%d: %d\n",ic,solve());
    }
    return 0;
}


 下面的legal参考了队友的,,,其实不该看人家代码太多啊,自己写思路更清晰,

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
using namespace std;

#define ls(rt) rt*2
#define rs(rt) rt*2+1
#define ll long long
#define ull unsigned long long
#define rep(i,s,e) for(int i=s;i<e;i++)
#define repe(i,s,e) for(int i=s;i<=e;i++)
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
#define OUT(s) freopen(s,"w",stdin)

const ull B=31;
const int MAXN = 100000+100;
char a[MAXN],b[MAXN];
ull ah[MAXN],bh[MAXN],base[MAXN];

int n,m;

int Find(int i, int j)
{
    int up=m+1-j,down=0,mid;//二分的是长度////
    ull tmpa,tmpb;
    while(up>down+1)
    {
        mid=(up+down)/2;
        tmpa=(i==0)?ah[i+mid-1]:ah[i+mid-1]-ah[i-1]*base[mid];///
        tmpb=(j==0)?bh[j+mid-1]:bh[j+mid-1]-bh[j-1]*base[mid];///
        if(tmpa == tmpb)down=mid;
        else up=mid;
    }
    return down;
}

int legal(int st)
{
    int prelen=0,j=0,use=0;
    for(int i=st;i<n && use<2 && j<m-1;i++,j++)//i<=n?
    {
        prelen=Find(i,j);
        i+=prelen;//
        j+=prelen;//
        use++;//记录二分的次数
        if(use>=2 && j<m-1)//重新写下
        {
            prelen=Find(i+1,j+1);
            j+=prelen;  //
            if(j>=m-1)return 1; //
            else return 0;
        }
    }
    return 1;//////
}

int solve()
{
    ah[0]=a[0],bh[0]=b[0],a[n+1]=0,b[m+1]=0;
    for(int i=1;i<=m;i++)
        bh[i]=bh[i-1]*B+b[i];
    for(int i=1;i<=n;i++)
        ah[i]=ah[i-1]*B+a[i];
    for(int i=0;i<=n-m;i++)
    {
        if(legal(i))return i;
    }
    return -1;
}

int main()
{
    //IN("BNUhash.txt");
    int ncase;
    scanf("%d",&ncase);
    base[0]=1;
    rep(i,1,MAXN)
        base[i]=base[i-1]*B;
    for(int ic=1;ic<=ncase;ic++)
    {
        scanf("%s%s",a,b);
        n=strlen(a);
        m=strlen(b);
        printf("Case #%d: %d\n",ic,solve());
    }
    return 0;
}


你可能感兴趣的:(BNU 34990 Justice String 2014 ACM-ICPC Beijing Invitational Programming Contest)