【第22期】观点:IT 行业加班,到底有没有价值?

poj2758 checking the text

原创 2016年05月31日 23:04:13

Description Wind’s birthday is approaching. In order to buy a really
really fantastic gift for her, Jiajia has to take a boring yet
money-making job - a text checker.

This job is very humdrum. Jiajia will be given a string of text what
is English letters and he must count the maximum number of letters
that can be matched, starting from two position of the current text
simultanously. The matching proceeds from left to right, one character
by one.

Even worse, sometimes the boss will insert some characters before,
after or within the text. Jiajia wants to write a program to do his
job automatically, this program should be fast enough, because there
are only few days to Wind’s birthday.

Input The first line of input file contains initial text.

The second line contains then number of commands n. And the following
n lines describe each command. There are two formats of commands:

I ch p: Insert a character ch before the p-th. if p is larger than the
current length of text, then insert at end of the text.

Q i j: Ask the length of matching started from the i-th and j-th
character of the initial text, which doesn’t include the inserted
characters.

You can assume that the length of initial text will not exceed 50000,
the number of I command will not exceed 200, the number of Q command
will not exceed 20000.

Output Print one line for each Q command, contain the max length of
matching.

哈希+二分求LCP,由于修改次数较少,暴力维护即可。
进阶版bzoj1014火星人,修改次数较多(点击此处查看题解),需要平衡树维护。

#include<cstdio>
#include<cstring>
char s[51000];
int f[51000];
unsigned long long h[51000],pw[51000];
int min(int x,int y)
{
    return x<y?x:y;
}
int main()
{
    int i,j,k,m,n,p=13131,q,x,y,z,T,l;
    unsigned long long x1,x2;
    char c;
    scanf("%s",s+1);
    scanf("%d",&T);
    n=l=strlen(s+1);
    pw[0]=1;
    for (i=1;i<=50500;i++)
      pw[i]=pw[i-1]*p;
    for (i=1;i<=l;i++)
      f[i]=i;
    for (i=1;i<=l;i++)
      h[i]=h[i-1]*p+s[i]-'a'+1;
    while (T--)
    {
        scanf("\n%c",&c);
        if (c=='Q')
        {
            scanf("%d%d",&x,&y);
            x=f[x];
            y=f[y];
            i=0;
            j=min(l-x,l-y)+1;
            while (i<j)
            {
                m=(i+j+1)/2;
                x1=h[x+m-1]-h[x-1]*pw[m];
                x2=h[y+m-1]-h[y-1]*pw[m];
                if (x1==x2) i=m;
                else j=m-1;
            }
            printf("%d\n",i);
        }
        else
        {
            scanf(" %c%d",&c,&x);
            if (x>l+1) x=l+1;
            for (i=l;i>=x;i--)
            {
                s[i+1]=s[i];                
            }
            s[x]=c;
            for (i=n;i>=1&&f[i]>=x;i--)
              f[i]++; 
            l++;
            for (i=x;i<=l;i++)
              h[i]=h[i-1]*p+s[i]-'a'+1;
        }
    }
} 
版权声明:本文为博主原创文章,未经博主允许不得转载,欢迎添加友链。 举报

相关文章推荐

[startrelatedarticles]

{relatedtitle}

{relateddes}
[endrelatedarticles] [startrelatedarticlesad1]

{relatedtitle}

{relateddes}
[endrelatedarticlesad1] [startrelatedarticlesad2]
{relateddes}
[endrelatedarticlesad2]
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)