codeforces 204E. Little Elephant and Strings(广义后缀自动机,Parent树)

本文介绍了一种使用后缀自动机和DFS序统计子串出现次数的算法,用于解决给定字符串集中每个字符串子串在所有字符串中出现K次以上的统计问题。通过构建后缀自动机并进行深度优先搜索,实现对子串的高效统计。

传送门在这里。

大意:

  给一堆字符串,询问每个字符串有多少子串在所有字符串中出现K次以上。

解题思路:

  这种子串问题一定要见后缀自动机Parent树Dfs序统计出现次数都是套路了吧。

这道题统计子串个数,那么可以发现,若一个节点所对应的子串出现了K次,那么其贡献就是len,不需要考虑重复。

因为即使出现重复也是在两个位置。

那么只需统计以每个点结束的子串就好了。

之前的Dfs序就很套路了。

只需再跑一遍字符串,更新答案就好了。

代码:

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 const int N=400000;
  5 struct sant{
  6     int tranc[26];
  7     int len;
  8     int pre;
  9 }s[N];
 10 struct pnt{
 11     int hd;
 12     int ind;
 13     int oud;
 14     int col;
 15     int ans;
 16 }p[N];
 17 struct ent{
 18     int twd;
 19     int lst;
 20 }e[N];
 21 struct int_2{
 22     int l;
 23     int r;
 24     int no;
 25 }d[N];
 26 int n,k;
 27 int siz;
 28 int dfn;
 29 int cnt;
 30 int fin;
 31 char tmp[N];
 32 int ll[N],rr[N];
 33 int col[N];
 34 int lst[N];
 35 int line[N];
 36 int str[N];
 37 int lowbit(int x)
 38 {
 39     return x&(-x);
 40 }
 41 void update(int pos,int x)
 42 {
 43     while(pos&&pos<=dfn)
 44     {
 45         line[pos]+=x;
 46         pos+=lowbit(pos);
 47     }
 48     return ;
 49 }
 50 int query(int pos)
 51 {
 52     int ans=0;
 53     while(pos)
 54     {
 55         ans+=line[pos];
 56         pos-=lowbit(pos);
 57     }
 58     return ans;
 59 }
 60 bool cmp(int_2 x,int_2 y)
 61 {
 62     return x.r<y.r;
 63 }
 64 void ade(int f,int t)
 65 {
 66     cnt++;
 67     e[cnt].twd=t;
 68     e[cnt].lst=p[f].hd;
 69     p[f].hd=cnt;
 70     return ;
 71 }
 72 void Insert(int c,int pl)
 73 {
 74     int nwp,nwq,lsp,lsq;
 75     nwp=++siz;
 76     s[nwp].len=s[fin].len+1;
 77     p[nwp].col=pl;
 78     for(lsp=fin;lsp&&!s[lsp].tranc[c];lsp=s[lsp].pre)
 79         s[lsp].tranc[c]=nwp;
 80     if(!lsp)
 81         s[nwp].pre=1;
 82     else{
 83         lsq=s[lsp].tranc[c];
 84         if(s[lsq].len==s[lsp].len+1)
 85             s[nwp].pre=lsq;
 86         else{
 87             nwq=++siz;
 88             s[nwq]=s[lsq];
 89             s[nwq].len=s[lsp].len+1;
 90             s[lsq].pre=s[nwp].pre=nwq;
 91             while(s[lsp].tranc[c]==lsq)
 92             {
 93                 s[lsp].tranc[c]=nwq;
 94                 lsp=s[lsp].pre;
 95             }
 96         }
 97     }
 98     fin=nwp;
 99 }
100 void Dfs(int x)
101 {
102     p[x].ind=++dfn;
103     col[dfn]=p[x].col;
104     for(int i=p[x].hd;i;i=e[i].lst)
105     {
106         int to=e[i].twd;
107         Dfs(to);
108     }
109     p[x].oud=++dfn;
110     col[dfn]=p[x].col;
111 }
112 int main()
113 {
114     scanf("%d%d",&n,&k);
115     if(k>n)
116     {
117         for(int i=1;i<=n;i++)
118             printf("%d ",0);
119         return 0;
120     }
121     fin=++siz;
122     for(int i=1;i<=n;i++)
123     {
124         ll[i]=rr[i-1]+1;
125         rr[i]=rr[i-1];
126         fin=1;
127         scanf("%s",tmp);
128         int len=strlen(tmp);
129         for(int j=0;j<len;j++)
130             str[++rr[i]]=tmp[j]-'a';
131         for(int j=ll[i];j<=rr[i];j++)
132         {
133             Insert(str[j],i);
134         }
135     }
136 
137     for(int i=2;i<=siz;i++)
138         ade(s[i].pre,i);
139     Dfs(1);    
140     for(int i=1;i<=siz;i++)
141         d[i]=(int_2){p[i].ind,p[i].oud,i};
142     std::sort(d+1,d+siz+1,cmp);
143     int r=1;
144     for(int i=1;i<=siz;i++)
145     {
146         while(r<=d[i].r)
147         {
148             if(!col[r])
149             {
150                 r++;
151                 continue;
152             }
153             if(lst[col[r]])
154                 update(lst[col[r]],-1);
155             update(r,1);
156             lst[col[r]]=r;
157             r++;
158         }
159         r--;
160         p[d[i].no].ans=query(d[i].r)-query(d[i].l-1);
161     }
162     for(int i=1;i<=n;i++)
163     {
164         long long int ans=0;
165         int root=1;
166         for(int j=ll[i];j<=rr[i];j++)
167         {
168             root=s[root].tranc[str[j]];
169             while(p[root].ans<k)
170                 root=s[root].pre;
171             ans+=(long long)s[root].len;
172         }
173         printf("%I64d ",ans);
174     }
175     puts("");
176     return 0;
177 }

 

转载于:https://www.cnblogs.com/blog-Dr-J/p/10083876.html

### Codeforces Div.2 比赛难度介绍 Codeforces Div.2 比赛主要面向的是具有基础编程技能到中级水平的选手。这类比赛通常吸引了大量来自全球不同背景的参赛者,包括大学生、高中生以及一些专业人士。 #### 参加资格 为了参加 Div.2 比赛,选手的评级应不超过 2099 分[^1]。这意味着该级别的竞赛适合那些已经掌握了一定算法知识并能熟练运用至少一种编程语言的人群参与挑战。 #### 题目设置 每场 Div.2 比赛一般会提供五至七道题目,在某些特殊情况下可能会更多或更少。这些题目按照预计解决难度递增排列: - **简单题(A, B 类型)**: 主要测试基本的数据结构操作和常见算法的应用能力;例如数组处理、字符串匹配等。 - **中等偏难题(C, D 类型)**: 开始涉及较为复杂的逻辑推理能力和特定领域内的高级技巧;比如图论中的最短路径计算或是动态规划入门应用实例。 - **高难度题(E及以上类型)**: 对于这些问题,则更加侧重考察深入理解复杂概念的能力,并能够灵活组合多种方法来解决问题;这往往需要较强的创造力与丰富的实践经验支持。 对于新手来说,建议先专注于理解和练习前几类较容易的问题,随着经验积累和技术提升再逐步尝试更高层次的任务。 ```cpp // 示例代码展示如何判断一个数是否为偶数 #include <iostream> using namespace std; bool is_even(int num){ return num % 2 == 0; } int main(){ int number = 4; // 测试数据 if(is_even(number)){ cout << "The given number is even."; }else{ cout << "The given number is odd."; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值