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 ...
随机推荐
- 【原创】说说JSON和JSONP,也许你会豁然开朗,含jQuery用例
说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域可以通过服 ...
- 测试的一些基本概念知识(TCP )
一.TCP报头部中的SYN.FIN.ACK: ACK : TCP协议规定,只有ACK=1时有效,也规定连接建立后所有发送的报文的ACK必须为1. SYN(SYNchronization) : 在连接建 ...
- 【搞机】Apple Pencil 开箱
前言 上次入手了新的iPad Pro .好开心呢. 然后发现官方的笔不错呢~ 后来,苹果官方的12期免息分期又回来啦~ 买买买!!! 上图 体验 官方的笔真的不愧叫Pencil ,完美模拟铅笔的手感. ...
- top命令中内存参数
总结:VIRT 虚拟内存中含有共享库.共享内存.栈.堆,所有已申请的总内存空间.RES 是进程正在使用的内存空间(栈.堆),申请内存后该内存段已被重新赋值.SHR 是共享内存正在使用的空间.SWA ...
- Daemon,Jos,定时器
--> FileSystemWatcher--> EventWaitHandle / AutoResetEvent / ManualResetEvent--> Mutex--> ...
- PHP下用Memcache 实现消息队列
Memcache 一般用于缓存服务.但是很多时候,比如一个消息广播系统,需要一个消息队列.直接从数据库取消息,负载往往不行.如果将整个消息队列用一个key缓存到memcache里面, 对于一个很大的消 ...
- 点滴积累【C#】---操作文件
操作文件 写入文件效果: 写入文件代码: private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) { try { File ...
- [Egret]长按截屏分享、分享截屏图片、本地存储
egret 分享有API可以把一个显示对象树渲染成一个位图纹理,我把它赋值给 HTML 的 Image 元素,就实现了图片的显示,在微信中,通过长按图片可以分享出去.当然在其他浏览器可以保存在本地. ...
- 1. DataBinding - offical tutorial
1. DataBinding - offical tutorial android DataBinding tutorial 构建环境 数据与布局文件的绑定 data binding 表达式 数据对象 ...
- cocos2dx3.1 win7安装步骤及编译到安桌
1. 下载及安装好工具 python2.7 ndk sdk ant cocos2dx3.1 eclipse 这些工具和曾经一样下载好放到同一文件夹下 加入环境变量 将python2.7的安装路径加入 ...