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;
        }
    }
} 
版权声明:本文为博主原创文章,未经博主允许不得转载,欢迎添加友链。

POJ-2758 checking the text(哈希+二分)

POJ-2758 checking the text(哈希+二分)题目: Wind’s birthday is approaching. In order to buy a really reall...

BZOJ 2258 pku2758 Checking the Text 文本校对 Splay+Hash

题目大意:给定一个字符串,多次插入一个字符和询问某两个后缀的LCP Splay+Hash。同1014 这逗比的询问。。。。。。 #include #include #include #in...
  • PoPoQQQ
  • PoPoQQQ
  • 2015年01月22日 10:52
  • 1254

【BZOJ】【P2258】【pku2758 Checking the Text 文本校对】【题解】【hash】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2258 明天就 了

POJ2394 Checking an Alibi 最短路

floyd算法去处理。但是至今还搞不明白为什么output limit exceed。。。。。。。。 匪夷所思。 #include using namespace std; const int...
  • wuyanyi
  • wuyanyi
  • 2011年09月01日 13:06
  • 279

POJ 2394 Checking an Alibi (最短路+Dijkstra)

POJ 2394 Checking an Alibi (最短路+Dijkstra) 函数模板。

POJ 2494 Acid Text 模拟

题目大意:给定CSS语言的图片合成器,要求编译运行并输出结果 首先过样例 这个应该问题不大 然后交上去WA 那么请注意以下问题 1.读入用char 然后构造成string 2.由于White Spac...
  • PoPoQQQ
  • PoPoQQQ
  • 2014年10月29日 14:08
  • 951

POJ2525-Text Formalization

转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6746954   大致题意:        首先说明,下面所述的“...

poj 1093 Formatting Text

/* * poj 1093 Formatting Text 数学模型: f[i]是以第i个单词为某行首,i到最后一个单词组成段落所能取得最小badness。 ...
  • baisung
  • baisung
  • 2012年05月20日 23:45
  • 581

【CJOJ1644】【洛谷2758】编辑距离

题面题目描述设A和B是两个字符串。我们要用最少的字符操作次数,将字符串A转换为字符串B。这里所说的字符操作共有三种: 1、删除一个字符; 2、插入一个字符; 3、将一个字符改为另一个字符; 皆...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj2758 checking the text
举报原因:
原因补充:

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