CF #244 (Div. 2)D
来源:互联网 发布:如何设计一个软件 编辑:程序博客网 时间:2024/05/16 17:20
我又来水sam了!!!
Description:
Solution:
Code:
#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <iostream>using namespace std;char str1[5010]="\0";char str2[5010]="\0";int len1=0,len2=0;struct trie{ int ch[26]; int fa; int Link;}tree[10010]={{{0},0,0}};int tp=0;int Np=0;struct sam{ int ch[26]; int lenth; int fail; int g1,g2; int Short;}pot[20010]={{{0},0,0,0,0,0}};int pp=0;int S[10010]={0};int rank[20010]={0};int New[10010]={0};int dui[10010]={0};int duip=0;void Add(int t,int c,int Pre){ int cnt=New[Pre];New[t]=++pp;pot[New[t]].lenth=pot[New[Pre]].lenth+1; for(;cnt!=0 && pot[cnt].ch[c]==0;cnt=pot[cnt].fail) pot[cnt].ch[c]=New[t]; if(cnt==0) pot[New[t]].fail=1; else { int q=pot[cnt].ch[c]; if(pot[q].lenth==pot[cnt].lenth+1) pot[New[t]].fail=q; else { pp++;pot[pp]=pot[q];pot[pp].lenth=pot[cnt].lenth+1; pot[q].fail=pot[New[t]].fail=pp; for(;cnt!=0 && pot[cnt].ch[c]==q;cnt=pot[cnt].fail) pot[cnt].ch[c]=pp; } } return;}int main(){ int ans=2e9; scanf("%s",str1+1); scanf("%s",str2+1); len1=strlen(str1+1); len2=strlen(str2+1); tp=1; int cnt=1; for(int i=1;i<=len1;i++) { int c=str1[i]-'a'; if(tree[cnt].ch[c]==0) { tree[cnt].ch[c]=++tp; tree[tp].fa=cnt; } cnt=tree[cnt].ch[c]; } cnt=1; for(int i=1;i<=len2;i++) { int c=str2[i]-'a'; if(tree[cnt].ch[c]==0) { tree[cnt].ch[c]=++tp; tree[tp].fa=cnt; } cnt=tree[cnt].ch[c]; } tree[1].Link=0; New[0]=++pp; dui[++duip]=1; for(int i=1;i<=duip;i++) { int u=dui[i]; for(int c=0;c<26;c++) if(tree[u].ch[c]!=0) { int v=tree[u].ch[c]; dui[++duip]=v; tree[v].Link=++Np; Add(Np,c,tree[u].Link); } } cnt=1; for(int i=1;i<=len1;i++) { int c=str1[i]-'a'; cnt=pot[cnt].ch[c]; pot[cnt].g1++; } cnt=1; for(int i=1;i<=len2;i++) { int c=str2[i]-'a'; cnt=pot[cnt].ch[c]; pot[cnt].g2++; } for(int i=1;i<=pp;i++) { S[pot[i].lenth]++; pot[i].Short=2e9; } pot[1].Short=0; for(int i=1;i<=len1 || i<=len2;i++) S[i]+=S[i-1]; for(int i=1;i<=pp;i++) rank[S[pot[i].lenth]--]=i; for(int i=1;i<=pp;i++) { int u=rank[i]; for(int c=0;c<26;c++) if(pot[u].ch[c]!=0) pot[pot[u].ch[c]].Short=min(pot[pot[u].ch[c]].Short,pot[u].Short+1); } for(int i=pp;i>=2;i--) { if(pot[rank[i]].g1==1 && pot[rank[i]].g2==1) ans=min(pot[rank[i]].Short,ans); pot[pot[rank[i]].fail].g1+=pot[rank[i]].g1; pot[pot[rank[i]].fail].g2+=pot[rank[i]].g2; } if(ans==2e9) ans=-1; cout<<ans<<endl; return 0;}
0 0
- CF #244 (Div. 2)D
- CF #211 (Div. 2)--D. Renting Bikes
- CF Round #240 (Div. 2) D
- CF#271 (Div. 2) D.(dp)
- CF 196 Div 2 D (树形dp)
- CF #77 Div 1 C ,Div 2 D
- cf 156 div.2 D. Mr. Bender and Square
- 【MZ】CF 358D - 358E #209 (Div. 2)
- cf-Round #216 (Div. 2) -D Valera and Fools-dfs
- CF 358D - 358E #209 (Div. 2)
- CF RCC 2014 Warmup (Div. 2) D. Cunning Gena
- CF RCC 2014 Warmup (Div. 2) D. Cunning Gena
- CF 题目集锦 PART 3 #262 div 2 D
- cf 267 div.2 D Fedor and Essay
- CF#271 (Div. 2) D Flowers.(dp)
- CF #274 (Div. 2) 479B - 479D
- cf Round #292 (Div. 2)D. Drazil and Tiles 构造
- cf Round #276 (Div. 2) D. Maximum Value
- 2012中国国家集训队命题答辩tree(伍一鸣)
- javascript canvas fps 帧速率计算
- spring 解读之 基本bean装配
- 在Mathematica中定义矩阵的半张量积运算
- 使用adb解锁
- CF #244 (Div. 2)D
- bzoj 3238: [Ahoi2013]差异
- (可视化模型表现)Visualizing Model Performance
- jQury animate操作 background-position 方法
- Android之TextView实现跑马灯的效果(笔记)
- C#求平均分和高于平均分的同学姓名。
- linux下动态库的符号冲突、隐藏和强制优先使用库内符号
- [Hnoi2013]游走(bzoj3143)
- java通过HTTP接收xml和返回xml