zoukankan      html  css  js  c++  java
  • COGS2217 papertask

    传送门

    以前看到这题的时候觉得是道好题啊……然而今天没多久就做出来了= =(B

    表示并没有看懂其他人写的是什么做法,感觉我的做法好奇怪……

    我的做法是这样的:

    首先给括号配对,不难发现所有括号串要么互不相交要么互相包含,也就是说它们形成了一个树形结构,暂且称之为括号树。

    比如括号序列[[[][]][[[]][][]]]的括号树就是这样的:

    每个节点的儿子就是剥掉最外层括号后的括号串,那么不难看出所有合法子串一定是某个节点的儿子顺序排列之后取连续的一段(必须是同一个节点的儿子,否则会因为最外层括号的缘故而使得子串不合法)。如果可以找到一种方法来区别本质不同的节点,那么直接把所有节点的儿子排列得到的字符串一块建一个广义后缀自动机就可以统计本质不同的子串个数了。最简单的方法就是利用哈希,然后就可以了……

    注意这里的字符集可能很大,一开始觉得广义后缀自动机的复杂度是错的。后来才反应过来,所有字符串总长度只有n,因此建广义后缀自动机也还是线性的。

    当然原串中连续的极长括号串也需要并在一起当作字符串插进去,这个稍微处理一下就行了。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<map>
     5 using namespace std;
     6 const int maxn=1000010;
     7 const unsigned long long base=131ull;
     8 void solve(int,int);
     9 void insert(int*,int);
    10 int expand(unsigned long long,int);
    11 unsigned long long gethash(int,int);
    12 int root,cnt=0,val[maxn]={0},par[maxn]={0};
    13 map<unsigned long long,int>go[maxn];
    14 unsigned long long h[maxn],pw[maxn];
    15 char str[maxn];
    16 long long ans=0;
    17 int n,a[maxn],s[maxn],top,t[maxn],last=0;
    18 int main(){
    19     freopen("papertask.in","r",stdin);
    20     freopen("papertask.out","w",stdout);
    21     int __size__=128<<20;
    22     char *__p__=(char*)malloc(__size__)+__size__;
    23     __asm__("movl %0, %%esp
    "::"r"(__p__));
    24     root=++cnt;
    25     scanf("%d",&n);
    26     scanf("%s",str+1);
    27     pw[0]=1;
    28     for(int i=1;i<=n;i++)pw[i]=pw[i-1]*base;
    29     for(int i=n;i;i--)h[i]=h[i+1]*base+str[i];
    30     for(int i=1;i<=n;i++){
    31         if(str[i]=='(')s[++top]=i;
    32         else{
    33             if(!top)continue;
    34             a[i]=s[top--];
    35             a[a[i]]=i;
    36         }
    37     }
    38     int x=1,last=0;
    39     while(x<=n){
    40         if(!a[x]){
    41             x++;
    42             continue;
    43         }
    44         last=x;
    45         while(a[a[x]+1])x=a[x]+1;
    46         solve(last,a[x]);
    47         x=a[x]+1;
    48     }
    49     printf("%lld
    ",ans);
    50     return 0;
    51 }
    52 void solve(int l,int r){
    53     if(l>r)return;
    54     int cnt=0,x=l;
    55     while(x<=r){
    56         t[cnt++]=gethash(x,a[x]-x+1);
    57         x=a[x]+1;
    58     }
    59     insert(t,cnt);
    60     x=l;
    61     while(x<=r){
    62         solve(x+1,a[x]-1);
    63         x=a[x]+1;
    64     }
    65 }
    66 void insert(int *a,int n){
    67     int x=root,i=0;
    68     //for(;i<n&&go[x].count(a[i]);i++)x=go[x][a[i]];
    69     for(;i<n;i++)x=expand(a[i],x);
    70 }
    71 int expand(unsigned long long c,int p){
    72     int np=++cnt;
    73     val[np]=val[p]+1;
    74     while(p&&!go[p].count(c)){
    75         go[p][c]=np;
    76         p=par[p];
    77     }
    78     if(!p)par[np]=root;
    79     else{
    80         int q=go[p][c];
    81         if(val[q]==val[p]+1)par[np]=q;
    82         else{
    83             int nq=++cnt;
    84             val[nq]=val[p]+1;
    85             go[nq]=go[q];
    86             par[nq]=par[q];
    87             par[np]=par[q]=nq;
    88             while(p&&go[p][c]==q){
    89                 go[p][c]=nq;
    90                 p=par[p];
    91             }
    92         }
    93     }
    94     ans+=val[np]-val[par[np]];
    95     return np;
    96 }
    97 inline unsigned long long gethash(int x,int l){return h[x]-h[x+l]*pw[l];}
    View Code

     

  • 相关阅读:
    帕累托分布(Pareto distributions)、马太效应
    Generalized normal distribution and Skew normal distribution
    Secondary NameNode 的作用
    127.0.0.1和0.0.0.0地址的区别
    50070只有本机可以访问(除本机外无法访问)
    SecureCRT SSH 语法高亮
    深入理解VMware虚拟机网络通信原理
    CentOS Virtual Machine 设置SSH主机登录
    路由器(交换机)的光口和电口
    ECC校验
  • 原文地址:https://www.cnblogs.com/hzoier/p/6638069.html
Copyright © 2011-2022 走看看