• CF119D(字符串-哈希求解(KMP求了半天,结果哈希更简单!))


    做提前一定要先比较KMP和哈希哪个更简单!!

    Let s be a string whose length equals n. Its characters are numbered from 0 to n - 1i and j are integers, 0 ≤ i < j < n. Let's define function f as follows:

    f(s, i, j) = s[i + 1... j - 1] + r(s[j... n - 1]) + r(s[0... i]).

    Here s[p... q] is a substring of string s, that starts in position p and ends in position q (inclusive); "+" is the string concatenation operator; r(x) is a string resulting from writing the characters of the x string in the reverse order. If j = i + 1, then the substring s[i + 1... j - 1] is considered empty.

    You are given two strings a and b. Find such values of i and j, that f(a, i, j) = b. Number i should be maximally possible. If for this i there exists several valid values of j, choose the minimal j.

    Input

    The first two input lines are non-empty strings a and b correspondingly. Each string's length does not exceed 106characters. The strings can contain any characters with ASCII codes from 32 to 126 inclusive.

    Output

    Print two integers ij — the answer to the problem. If no solution exists, print "-1 -1" (without the quotes).

    Sample test(s)
    input
    Die Polizei untersucht eine Straftat im IT-Bereich.
    untersucht eine Straftat.hciereB-TI mi ieziloP eiD
    output
    11 36
    input
    cbaaaa
    aaaabc
    output
    4 5
    input
    123342
    3324212
    output
    -1 -1

    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #include<string>
    #include<cstring>
    #include<set>
    #include<map>
    #include<list>
    #include<queue>
    #include<vector>
    #define tree int o,int l,int r
    #define lson o<<1,l,mid
    #define rson o<<1|1,mid+1,r
    #define lo o<<1
    #define ro o<<1|1
    #define ULL unsigned long long
    #define LL long long
    #define UI unsigned int
    #define inf 0x7fffffff
    #define eps 1e-7
    #define N 1000009
    #define f 97531997
    using namespace std;
    int T,n,m,k,lena,lenb,x,y,ok;
    char a[N],b[N];
    ULL ha[N],fa[N],hb[N],ep[N];
    int ll[N],rr[N];
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("ex.in","r",stdin);
    #endif
        int ncase=0;
        while (gets(a+1))
        {
            gets(b+1);
            lena=strlen(a+1);
            lenb=strlen(b+1);
            if(lena!=lenb)
            {
                puts("-1 -1");continue;
            }
            n=lena;
            ha[0]=fa[0]=hb[0]=0;
            ep[0]=1;
            for(int i=1;i<=n;i++)
            {
                ep[i]=ep[i-1]*f;
                ha[i]=ha[i-1]*f+a[i];
                hb[i]=hb[i-1]*f+b[i];
                fa[i]=fa[i-1]*f+a[n-i+1];
            }
            for(int i=1;i<=n;i++)
            {
                int l=0,r=n+1-i;
                while(l<r)//二分求长度!
                {
                    int mid=(l+r+1)>>1;
                    if(fa[mid]==hb[i+mid-1]-hb[i-1]*ep[mid])
                    l=mid;
                    else
                    r=mid-1;
                }
                rr[i]=l;
            }
            for(int i=1,j=1;i<n;i++)
            {
                for(;j<i+rr[i];j++)
                ll[j]=i;
            }
            for(int i=1;i<n;i++)
            {
                if(a[i]!=b[n+1-i])break;
                if(ll[n-i]>n-i||ll[n-i]==0)continue;//WA,ll[n-i]==0
    
                int len=n-i-ll[n-i]+1;
                if(hb[ll[n-i]-1]==ha[n-len]-ha[i]*ep[n-i-len])
                {
                    x=i,y=n-len+1;
                }
            }
            printf("%d %d
    ",x-1,y-1);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    开源数据访问组件Smark.Data 1.8
    .NET应用加载容器KGlue
    TCP&UDP压力测试工具
    使用Beetle.Express简单构建高吞吐的TCP&UDP应用
    通过分析内存来优化.NET程序
    winsock I/O模型
    C++各大有名库的介绍
    深入研究 STL Deque 容器An InDepth Study of the STL Deque Container (By Nitron)
    C C++编程子资料库(小程序)
    VSS服务器安装配置(比较完整的一篇VSS服务器配置的文章)
  • 原文地址:https://www.cnblogs.com/sbaof/p/3365835.html
Copyright © 2020-2023  润新知