SPOJ Number of Palindromes(回文树)
| Time Limit: 100MS | Memory Limit: 1572864KB | 64bit IO Format: %lld & %llu |
Description
Each palindrome can be always created from the other palindromes, if a single character is also a palindrome. For example, the string "malayalam" can be created
by some ways:
* malayalam = m + ala + y + ala + m
* malayalam = m + a + l + aya + l + a + m
We want to take the value of function NumPal(s) which is the number of different palindromes that can be created using the string S by the above method. If the same palindrome occurs more than once then all of them should be counted separately.
Input
The string S.
Output
The value of function NumPal(s).
Limitations
0 < |s| <= 1000
Example
Input:
malayalam
Output:
15
Hint
| Added by: | The quick brown fox jumps over the lazy dog |
| Date: | 2010-10-18 |
| Time limit: | 0.100s-0.170s |
| Source limit: | 50000B |
| Memory limit: | 1536MB |
| Cluster: | Cube (Intel G860) |
| Languages: | All |
| Resource: | Udit Agarwal |
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
#define MAX 1005
struct Node
{
int next[26];
int len;
int sufflink;
int num;
}tree[MAX];
char s[MAX]; int num;
int suff;
bool addLetter(int pos)
{
int cur=suff,curlen=0;
int let=s[pos]-'a'; while(1)
{
curlen=tree[cur].len;
if(pos-1-curlen>=0&&s[pos-1-curlen]==s[pos])
break;
cur=tree[cur].sufflink;
}
if(tree[cur].next[let])
{
suff=tree[cur].next[let];
return false;
}
num++;
suff=num;
tree[num].len=tree[cur].len+2;
tree[cur].next[let]=num;
if(tree[num].len==1)
{
tree[num].sufflink=2;
tree[num].num=1;
return true;
}
while(1)
{
cur=tree[cur].sufflink;
curlen=tree[cur].len;
if(pos-1-curlen>=0&&s[pos-1-curlen]==s[pos])
{
tree[num].sufflink=tree[cur].next[let];
break;
}
}
tree[num].num=1+tree[tree[num].sufflink].num;
return true; }
void initTree()
{
num=2;suff=2;
tree[1].len=-1;tree[1].sufflink=1;
tree[2].len=0;tree[2].sufflink=1;
}
int main()
{
scanf("%s",s);
int len=strlen(s);
initTree();
long long int ans=0;
for(int i=0;i<len;i++)
{
addLetter(i);
ans+=tree[suff].num;
}
printf("%d\n",ans);
return 0;
}
| Time Limit: 100MS | Memory Limit: 1572864KB | 64bit IO Format: %lld & %llu |
Description
Each palindrome can be always created from the other palindromes, if a single character is also a palindrome. For example, the string "malayalam" can be created
by some ways:
* malayalam = m + ala + y + ala + m
* malayalam = m + a + l + aya + l + a + m
We want to take the value of function NumPal(s) which is the number of different palindromes that can be created using the string S by the above method. If the same palindrome occurs more than once then all of them should be counted separately.
Input
The string S.
Output
The value of function NumPal(s).
Limitations
0 < |s| <= 1000
Example
Input:
malayalam
Output:
15
Hint
| Added by: | The quick brown fox jumps over the lazy dog |
| Date: | 2010-10-18 |
| Time limit: | 0.100s-0.170s |
| Source limit: | 50000B |
| Memory limit: | 1536MB |
| Cluster: | Cube (Intel G860) |
| Languages: | All |
| Resource: | Udit Agarwal |
SPOJ Number of Palindromes(回文树)的更多相关文章
- SP7586 NUMOFPAL - Number of Palindromes(回文树)
题意翻译 求一个串中包含几个回文串 题目描述 Each palindrome can be always created from the other palindromes, if a single ...
- 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)
[SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...
- 【CF245H】Queries for Number of Palindromes(回文树)
[CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...
- CF245H Queries for Number of Palindromes(回文树)
题意翻译 题目描述 给你一个字符串s由小写字母组成,有q组询问,每组询问给你两个数,l和r,问在字符串区间l到r的字串中,包含多少回文串. 输入格式 第1行,给出s,s的长度小于5000 第2行给出q ...
- URAL 2040 Palindromes and Super Abilities 2(回文树)
Palindromes and Super Abilities 2 Time Limit: 1MS Memory Limit: 102400KB 64bit IO Format: %I64d ...
- 【Aizu2292】Common Palindromes(回文树)
[Aizu2292]Common Palindromes(回文树) 题面 Vjudge 神TMD日语 翻译: 给定两个字符串\(S,T\),询问\((i,j,k,l)\)这样的四元组个数 满足\(S[ ...
- 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities
Palindromes and Super Abilities Problem's Link: http://acm.timus.ru/problem.aspx?space=1&num=19 ...
- Gym - 101806Q:QueryreuQ(回文树)
A string is palindrome, if the string reads the same backward and forward. For example, strings like ...
- HDU 5157 Harry and magic string(回文树)
Harry and magic string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- Cocos2d-X中的ZORDER和Tag
ZORDER:是描写叙述渲染顺序的值,每一个CCNode都有ZORDER,默认是0 ZORDER越大,越后面绘制 假设ZORDER同样.那么看arrival顺序.先增加的节点先绘制 ZORDER仅仅在 ...
- python oop面向对象笔记
#coding:utf-8 class Person(object): def __init__(self,name,wage): self.name = name self.wage = wage ...
- access denied XXXXXXXXXXXX
这个异常是做支付的时候出现的,貌似是没有权限访问之类的,网上查了有N个解决的办法,我写一下我解决的办法吧....... 出现的异常具体: java.security.AccessControlExce ...
- 实验c语言不同类型的指针互用(不推荐只是学习用)
#include <stdio.h> int main(int argc, char *argv[]) { printf("Hello, world\n"); ]; i ...
- NGUI ScrollView中的Bounds
获取到的Bounds值是固定的,是因为Bounds区域的计算是被动计算,需要主动调用使其刷新 scrollView.InvalidateBounds(); 另外Bounds的Min和Max似乎和NGU ...
- atitit.nfc 身份证 银行卡 芯片卡 解决方案 attilax总结
atitit.nfc 身份证 银行卡 芯片卡 解决方案 attilax总结 1. nfc(近距离无线通讯技术) 1 2. 工作模式 1 3. NFC 蓝牙 红外具体对比如下表: 2 4. TypeA ...
- centos 系统管理维护指南
# centos 系统管理维护指南 centos系统是服务器的首选系统,系统运维支持需要的内容汇总整理如下. ### 系统管理------------------------------ 查看系统版本 ...
- RxBinding -- 官网说明
RxBinding -- 官网说明 新建 模板 小书匠 作用 组件 平台绑定 support-v4 绑定 appcompact-v7 绑定 design 库绑定 recyclerview-v7 绑定 ...
- (译)Getting Started——1.3.1 Incorporating the Data(合并数据)
在实现完应用的行为后,需要创建数据模型来支撑应用的界面.应用的数据模型定义了应用中数据的维护方法.数据模型可以是一个基本的dictionary类型,也可以是复杂的数据库,范围很广.好的数据模型可以更为 ...
- 1.3 Services - 服务
服务是一种应用组件,它可以在后台执行耗时的操作,它是没有用户界面的.其它的应用组件都可以开启一个服务,服务开启后,即使用户离开了应用,服务仍然可以在后台运行.此外,绑定到服务的组件可以与服务进行交互, ...