代码暂存
发布时间:2020-12-16 10:48:57 所属栏目:百科 来源:网络整理
导读:1 #includebits/stdc++.h 2 using namespace std; 3 const int maxn = 3e5 + 5 ; 4 #define ll long long 5 struct Tree 6 { 7 int len,fail,go[ 26 ]; // Go边指向添加字符后达到的状态,fail边指向当前状态字符串的最长回文后缀,类似于SAM 8 }tr[maxn]; 9
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn = 3e5 + 5; 4 #define ll long long 5 struct Tree 6 { 7 int len,fail,go[26];//Go边指向添加字符后达到的状态,fail边指向当前状态字符串的最长回文后缀,类似于SAM 8 }tr[maxn]; 9 char s[maxn]; 10 int len,last,tot,nowi,cnt[maxn];//Cnt[i]表示状态i在母串中出现的次数 11 void init() 12 { 13 tr[0].len = 0,tr[1].len = -1; 14 tot = 1,last = 0; 15 tr[0].fail = 1; 16 nowi=0; 17 } 18 int get(int now) 19 { 20 while (s[nowi - tr[now].len - 1] != s[nowi]) now = tr[now].fail; 21 return now; 22 } 23 void add(int c) 24 { 25 nowi++; 26 int t = get(last); 27 if (!tr[t].go[c]) 28 { 29 tr[++ tot].len = tr[t].len + 2; 30 tr[tot].fail = tr[get(tr[t].fail)].go[c]; 31 tr[t].go[c] = tot; 32 } 33 last = tr[t].go[c]; 34 cnt[last] ++; 35 } 36 ll getans() 37 { 38 ll ans = 0; 39 for (int i = tot; i > 1; i --) 40 { 41 cnt[tr[i].fail] += cnt[i]; 42 ans = max(ans,1LL*tr[i].len*cnt[i]); 43 } 44 return ans; 45 } 46 int main() 47 { 48 scanf("%s",s + 1); 49 len = strlen(s + 1); 50 init(); 51 for (int i = 1; i <= len; i ++) add(s[i] - ‘a‘); 52 printf("%lld",getans()); 53 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 《从零开始学Swift》学习笔记(Day 20)――函数中参数的传
- 使用AJAX的ASP.NET MVC加载页面
- c# – 如何突出显示/选择wpf文本框中没有焦点的文本?
- Cocos2d-js 3.0 jsb环境调用底层Objective-C代码
- vb.net 教程 3-4 窗体编程 公共控件10 TreeView 1
- Ajax 生成流文件下载
- Oracle DBA数据库高级工程师培训(上部)项目实施+备份恢复
- UserLoginAction-login-validation.xml Connection timed o
- 灵活顶点格式--Flexible Vertex Format, FVF
- Vue.js每天必学之组件与组件间的通信