HDU 3948 The Number of Palindromes
2015-10-27 13:33
537 查看
The Number of Palindromes
Time Limit: 3000msMemory Limit: 262144KB
This problem will be judged on HDU. Original ID: 3948
64-bit integer IO format: %I64d Java class name: Main
Now, you are given a string S. We want to know how many distinct substring of S which is palindrome.
Input
The first line of the input contains a single integer T(T<=20), which indicates number of test cases.Each test case consists of a string S, whose length is less than 100000 and only contains lowercase letters.
Output
For every test case, you should output "Case #k:" first in a single line, where k indicates the case number and starts at 1. Then output the number of distinct substring of S which is palindrome.Sample Input
3 aaaa abab abcd
Sample Output
Case #1: 4 Case #2: 4 Case #3: 4
Source
2011 Multi-University Training Contest 11 - Host by UESTC解题:Palindromic Tree
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; struct PalindromicTree{ int son[maxn][26],fail[maxn],len[maxn],s[maxn]; int tot,last,n; int newnode(int slen = 0){ memset(son[tot],0,sizeof son[tot]); len[tot] = slen; return tot++; } void init(){ tot = last = n = 0; newnode(0); newnode(-1); fail[0] = fail[1] = 1; s = -1; } int getFail(int x){ while(s[n - len[x] -1] != s ) x = fail[x]; return x; } void extend(int c){ s[++n] = c; int cur = getFail(last); if(!son[cur][c]){ int x = newnode(len[cur] + 2); fail[x] = son[getFail(fail[cur])][c]; son[cur][c] = x; } last = son[cur][c]; } }pt; char str[maxn]; int main(){ int kase,cs = 1; scanf("%d",&kase); while(kase--){ scanf("%s",str); pt.init(); for(int i = 0; str[i]; ++i) pt.extend(str[i] - 'a'); printf("Case #%d: %d\n",cs++,pt.tot - 2); } return 0; }
View Code
相关文章推荐
- 黑马程序员****OC语言基础****有参和无参方法的调用和实现
- 一看就会Android之单选按钮和复选框的事件监听
- 跟我一起学习windows驱动编程(对象和句柄)
- UIView
- AES加解密
- 常用正则及表单校验工具类
- android 使静音模式下音乐也静音,切换到其他模式恢复
- 彻底理解强连通!!
- TalkingData
- C语言 isdigit
- Oracle数据类型
- 苏州地铁线路查询接口 地铁线路实时详细信息
- android 在通话状态下,当有通知来时(如低电量提示、wifi 连接,等),没有通知音
- 动态绑定一
- iOS QQ第三方登录实现
- NiftyDialogEffects-多种弹出效果的对话框
- js中substring和substr的用法
- js控制audio
- webrc ios 打开h264 硬件编解码
- android 6.0 使用HttpClient