zoukankan      html  css  js  c++  java
  • POJ2758 Checking the Text

    题目链接:https://vjudge.net/problem/POJ-2758

    题目大意:

      先给出一串原始字符串,在此基础上执行两种操作:

      1、在第 p 个字符前添加字符 ch,如果 p 比现字符串的长度 nowlen 大,则添加到现字符串的末尾。注意,这里的所有位置都是相对于变化后的字符串而言的!

      2、查询第 i 个和第 j 个后缀的最长公共前缀。注意,这里的位置是相对于原始字符串而言的。

    解题思路:

      这道题真的可以说是最近几个月我做的最痛苦的一道题之一了。真的很煎熬。

      一开始,我的思路是后缀数组加模拟(因为实在 kuangbin 的后缀数组专题里面碰到的嘛~)。发现其实在这种做法中后缀数组作用并不大,主要的工作都在于模拟字符的添加和最长公共前缀的查询,后缀数组只不过是起到了加速的作用,而且模拟的过程及其之麻烦,感觉不是很自然,所以就去查了一眼题解,发现还真的有人用后缀数组做的,扫了几眼他的思路,好像也是后缀数组加暴力的做法。。。所以我决定自己动手写一版。。。然后。。。一场悲剧。。。真的是各种不好写。。。好不容易把它调到可以过我能找到的所有样例和我自己造的几组数据。。。但还是各种WA。。。真的好痛苦。。。整整一天都调不出来。。。在此贴上代码,以后哪天再回过头来看看吧:

      1 #include <iostream>
      2 #include <cstring>
      3 #include <cstdio>
      4 #include <set>
      5 #include <vector>
      6 #include <algorithm>
      7 
      8 using namespace std;
      9 const int maxn=50000+3,inf=0x7fffffff;
     10 char inp[maxn];
     11 int s[maxn];
     12 int n,k;
     13 int Rank[maxn],sa[maxn],tmp[maxn],height[maxn];
     14 bool compare_sa(int i,int j){
     15     if(Rank[i]!=Rank[j])    return Rank[i]<Rank[j];
     16     else{
     17         int ri=i+k<=n?Rank[i+k]:-1;
     18         int rj=j+k<=n?Rank[j+k]:-1;
     19         return ri<rj;
     20     }
     21 }
     22 void construct_sa(){
     23     for(int i=0;i<=n;i++){
     24         sa[i]=i;
     25         Rank[i]=i<n?s[i]:-1;
     26     }
     27     for(k=1;k<=n;k*=2){
     28         sort(sa,sa+n+1,compare_sa);
     29         tmp[sa[0]]=0;
     30         for(int i=1;i<=n;i++){
     31             tmp[sa[i]]=tmp[sa[i-1]]+(compare_sa(sa[i-1],sa[i])?1:0);
     32         }
     33         for(int i=0;i<=n;i++){
     34             Rank[i]=tmp[i];
     35         }
     36     }
     37 }
     38 void construct_height(){
     39     int h=0;
     40     height[0]=0;
     41     for(int i=0;i<n;i++){
     42         int j=sa[Rank[i]-1];
     43         if(h>0) h--;
     44         for(;j+h<n&&i+h<n;h++){
     45             if(s[j+h]!=s[i+h])  break;
     46         }
     47         height[Rank[i]-1]=h;
     48     }
     49 }
     50 vector<int> add[50003];
     51 set<int> inserts;
     52 int main()
     53 {
     54     freopen("in.txt","r",stdin);
     55     char ord[5],ins[5];
     56     int have,a,b;
     57     scanf("%s",inp);
     58     n=strlen(inp);
     59     int nowlen=n;
     60     for(int i=0;i<n;i++)    s[i]=inp[i]-'A';
     61     construct_sa();
     62     construct_height();
     63     scanf("%d",&have);
     64     while(have--){
     65 //        for(int i=0;i<n;i++){
     66 //            if(inserts.count(i)){
     67 //                for(int j=0;j<add[i].size();j++)    printf("%c",'A'+add[i][j]);
     68 //            }
     69 //            printf("%c",'A'+s[i]);
     70 //        }
     71 //        for(int i=0;i<add[n].size();i++)    printf("%c",'A'+add[n][i]);
     72 //        printf("
    ");
     73         scanf("%s",ord);
     74         if(ord[0]=='Q'){
     75             scanf("%d%d",&a,&b);    //a<b
     76             a--,b--;
     77             if(a>b) swap(a,b);
     78             int same=inf;
     79             if(Rank[a]<Rank[b]){
     80                 for(int i=Rank[a];i<Rank[b];i++){
     81                     if(height[i]<same)  same=height[i];
     82                 }
     83             }
     84             else{
     85                 for(int i=Rank[b];i<Rank[a];i++){
     86                     if(height[i]<same)  same=height[i];
     87                 }
     88             }
     89 
     90             bool go=true;
     91             int ans=0,last_a=a,last_b=b,last_a_in=0,last_b_in=0;
     92             set<int>::iterator k1=inserts.upper_bound(a);
     93             set<int>::iterator k2=inserts.upper_bound(b);
     94             int p1=*k1,p2=*k2;
     95             if(k1==inserts.end())   p1=inf;
     96             if(k2==inserts.end())   p2=inf;
     97             if(p1-a>same && p2-b>same){
     98                 printf("%d
    ",same);
     99                 continue;
    100             }
    101             while(go){
    102 //                printf("p1: %d, p2: %d, ans: %d, last_b: %d, last_a: %d
    ",p1,p2,ans,last_b,last_a);
    103                 if(p1-a>same && p2-b>same){
    104                     while(last_a<n&&last_b<n&&s[last_a++]==s[last_b++]) ans++;
    105                     break;
    106                 }
    107                 if(last_a_in&&last_b_in){
    108                     for(;last_a_in<add[p1].size()&&last_b_in<add[p2].size();last_a_in++,last_b_in++){
    109                         if(add[p1][last_a_in]!=add[p2][last_b_in]){
    110                             go=false;   break;
    111                         }else    ans++;
    112                     }if(!go)    break;
    113                     if(last_a_in==add[p1].size()){
    114                         k1=inserts.upper_bound(p1);
    115                         last_a=p1,last_a_in=0;
    116                         p1=*k1;
    117                         if(k1==inserts.end())   p1=inf;
    118                     }
    119                     if(last_b_in==add[p2].size()){
    120                         k2=inserts.upper_bound(p2);
    121                         last_b=p2,last_b_in=0;
    122                         p2=*k2;
    123                         if(k2==inserts.end())   p2=inf;
    124                     }
    125                 }else if(last_a_in){
    126                     for(;last_a_in<add[p1].size()&&last_b<n&&last_b<p2;last_a_in++,last_b++){
    127                         if(add[p1][last_a_in]!=s[last_b]){
    128                             go=false;   break;
    129                         }else    ans++;
    130                     }if(!go)    break;
    131                     if(last_a_in==add[p1].size()&&last_b==p2){
    132                         k1=inserts.upper_bound(p1);
    133                         last_a=p1,last_a_in=0;
    134                         p1=*k1;
    135                         if(k1==inserts.end())   p1=inf;
    136 
    137                         if(add[p2][last_b_in++]==s[last_a++])   ans++;
    138                         else{
    139                             go=false;
    140                         }
    141                     }else if(last_a_in==add[p1].size()){
    142                         k1=inserts.upper_bound(p1);
    143                         last_a=p1,last_a_in=0;
    144                         p1=*k1;
    145                         if(k1==inserts.end())   p1=inf;
    146                     }else{
    147                         if(last_a_in<add[p1].size()&&last_b_in<add[p2].size()&&add[p2][last_b_in++]==add[p1][last_a_in++])   ans++;
    148                         else{
    149                             go=false;
    150                         }
    151                     }if(!go)    break;
    152                 }else if(last_b_in){
    153                     for(;last_b_in<add[p2].size()&&last_a<n&&last_a<p1;last_b_in++,last_a++){
    154                         if(add[p2][last_b_in]!=s[last_a]){
    155                             go=false;   break;
    156                         }else    ans++;
    157                     }if(!go)    break;
    158                     if(last_b_in==add[p2].size()&&last_a==p1){
    159                         k2=inserts.upper_bound(p2);
    160                         last_b=p2,last_b_in=0;
    161                         p2=*k2;
    162                         if(k2==inserts.end())   p2=inf;
    163 
    164                         if(add[p1][last_a_in++]==s[last_b++])   ans++;
    165                         else{
    166                             go=false;
    167                         }
    168                     }else if(last_b_in==add[p2].size()){
    169                         k2=inserts.upper_bound(p2);
    170                         last_b=p2,last_b_in=0;
    171                         p2=*k2;
    172                         if(k2==inserts.end())   p2=inf;
    173                     }else{
    174                         if(add[p1][last_a_in++]==add[p2][last_b_in++])   ans++;
    175                         else{
    176                             go=false;
    177                         }
    178                     }if(!go)    break;
    179                 }else{
    180                     if(p1-a<p2-b){
    181                         ans+=p1-last_a;
    182                         last_b+=(p1-last_a);
    183                         last_a=p1;
    184                         if(add[p1][last_a_in++]==s[last_b++])   ans++;
    185                         else
    186                             go=false;
    187                     }
    188                     else if(p1-a>p2-b){
    189                         ans+=p2-last_b;
    190                         last_a+=(p2-last_b);
    191                         last_b=p2;
    192                         if(add[p2][last_b_in++]==s[last_a++])   ans++;
    193                         else
    194                             go=false;
    195                     }
    196                     else{
    197                         if(p1<=n){
    198                             ans+=p1-last_a;
    199                             last_a=p1,last_b=p2;
    200                             if(add[p1][last_a_in++]==add[p2][last_b_in++])  ans++;
    201                             else
    202                                 go=false;
    203                         }
    204                         else{
    205                             while(last_a<n&&last_b<n&&s[last_a++]==s[last_b++]) ans++;
    206                             go=false;
    207                         }
    208                     }
    209                 }
    210             }
    211             printf("%d
    ",ans);
    212         }
    213         else{
    214             scanf("%s %d",ins,&a);
    215             a--;
    216 //          printf("nowlen: %d, a: %d
    ",nowlen,a);
    217             if(a>=nowlen){
    218                 add[n].push_back(ins[0]-'A');
    219                 inserts.insert(n);
    220             }
    221             else{
    222                 int now=0;
    223                 set<int>::iterator k3=inserts.upper_bound(now);
    224                 int last=*k3;
    225                 if(k3==inserts.end())   last=inf;
    226   //              printf("now: %d, last: %d
    ",now,last);
    227                 if(last>a){
    228                     add[a].push_back(ins[0]-'A');
    229                     inserts.insert(a);
    230                 }
    231                 else{
    232                     while(1){
    233                         now=last-1;
    234  //                       printf("a: %d
    ",a);
    235                         if(a-now>add[last].size()+1){
    236                             a-=add[last].size();
    237                             now++;
    238                             k3=inserts.upper_bound(now);
    239                             last=*k3;
    240                             if(k3==inserts.end())   last=inf;
    241   //                          printf("a: %d, now: %d, last: %d
    ",a,now,last);
    242                         }
    243                         else if(a-now==add[last].size()+1){
    244                             add[last].push_back(ins[0]-'A');
    245                             break;
    246                         }
    247                         else{
    248   //                          printf("pos: %d
    ",a-now);
    249                             add[last].insert(add[last].begin()+a-now-1,ins[0]-'A');
    250                             break;
    251                         }
    252                         if(a-now<last-now){
    253                             add[a].push_back(ins[0]-'A');
    254                             inserts.insert(a);
    255                             break;
    256                         }
    257                     }
    258                 }
    259             }
    260             ++nowlen;
    261         }
    262     }
    263     return 0;
    264 }
    View Code

      后来实在受不了了,在网上学了一种哈希的做法,二分又写挂了。。。好累。。。

    AC代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 const int maxn=100000;
     6 unsigned long long hashs[maxn],pw[maxn];
     7 char inp[maxn];
     8 int len,old[maxn],nowlen;
     9 void build(int s){
    10     for(int i=s;i<=nowlen;i++) 
    11         hashs[i]=hashs[i-1]*131+inp[i];
    12 }
    13 
    14 int query(int a,int b){
    15     if(a>b) swap(a,b);
    16     int l=0,r=nowlen-b+1,mid;   //此处要把 l 和 r 之间的距离尽量压缩,不然会WA
    17     int ans=0;
    18     while(l<r){
    19         mid=(l+r+1)/2;  //此处令mid稍微右偏,然后 r 修改的时候用 r=mid-1,稍微左偏,提高精度,不然也会WA
    20         if(hashs[a+mid-1]-hashs[a-1]*pw[mid]==hashs[b+mid-1]-hashs[b-1]*pw[mid])    l=mid,ans=mid;
    21         else    r=mid-1;
    22     }
    23     return ans;
    24 }
    25 int main(){
    26     int have,a,b;
    27     char ord[5],ins[5];
    28     scanf("%s",inp+1);
    29     scanf("%d",&have);
    30     nowlen=strlen(inp+1);
    31     len=nowlen;
    32     pw[0]=1;
    33     for(int i=1;i<=len+250;i++) pw[i]=pw[i-1]*131;
    34     for(int i=1;i<=len;i++) old[i]=i;
    35     build(1);
    36     for(int j=0;j<have;j++){
    37         scanf("%s",ord);
    38         if(ord[0]=='Q'){
    39             scanf("%d%d",&a,&b);
    40             printf("%d
    ",query(old[a],old[b]));
    41         }
    42         else{
    43             scanf("%s %d",ins,&a);
    44             if(a>nowlen)    a=nowlen+1;
    45             for(int i=nowlen;i>=a;i--)   inp[i+1]=inp[i];
    46             inp[a]=ins[0];nowlen++;
    47             build(a);
    48             for(int i=len;i>=1;i--){
    49                 if(old[i]>=a)   old[i]++;
    50                 else    break;
    51             }
    52         }
    53     }
    54     return 0;
    55 }
    “这些年我一直提醒自己一件事情,千万不要自己感动自己。大部分人看似的努力,不过是愚蠢导致的。什么熬夜看书到天亮,连续几天只睡几小时,多久没放假了,如果这些东西也值得夸耀,那么富士康流水线上任何一个人都比你努力多了。人难免天生有自怜的情绪,唯有时刻保持清醒,才能看清真正的价值在哪里。”
  • 相关阅读:
    vue 启动报错:`TypeError: Cannot read property 'range' of null`
    手动创建自己的npm包
    uni-app 拦截页面传参
    uni-app的vue.config.js
    插入排序
    选择排序
    设计模式--享元模式
    设计模式--代理模式
    原型链图片
    深度优先遍历和广度优先遍历
  • 原文地址:https://www.cnblogs.com/Blogggggg/p/7807023.html
Copyright © 2011-2022 走看看