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 ...
随机推荐
- 关于new与=号创建对象的区别
(1)先定义一个名为str的对String类的对象引用变量:String str: (2)[在[栈]中查找有没有存放值为"abc"的地址,如果没有,则开辟一个存放字面值为" ...
- javascript开源大全
javascript开源大全 Ajax框架-jQuery 可视化HTML编辑器-CKEditor 国产jQuery-UI框架-(jUI)-DWZ 网页开发FireFox插件-Firebug 服务器端的 ...
- mongodb - mongotop
mongodb - mongotop # ./mongotop 2016-03-12T16:37:32.141+0800 connected to: 127.0.0.1 ns total read w ...
- Spring学习笔记(四)-- Spring事务全面分析
通过本系列的文章对Spring的介绍,我们对Spring的使用和两个核心功能IOC.AOP已经有了初步的了解,结合我个人工作的情况,因为项目是金融系 统.那对事务的控制是不可缺少的.而且是很严格的控制 ...
- Solr3.6.2和Solr4.9.0经常使用配置
tomcat 以tomcat 7为例,位置/work/apache-tomcat-7.0.55 Solr 3.6.2 基本配置 Solr 3.6.2.须要JDK 6/JDK7支持. 下载Solr 3. ...
- scut协议配置工具初始化的一些问题
1.如果点击协议配置工具左上角的项目按钮没反应,说明数据库没有正确配置. 2.数据库创建脚本运行如果乱码,把utf-8改成ansi 3.如果ContractDB不存在就自行创建数据库. 4.使用sql ...
- Atitit.用户权限服务 登录退出功能
Atitit.用户权限服务 登录退出功能 参数说明 /com.attilax/user/loginOut.jsp?url="+url Utype=mer 作者:: ★(attilax)&g ...
- 设置phpcms v9黄页模块作为首页方法
如果我们根据需要,想把黄页作为单独的网站,我们可以用模块化安装,并且首页设置,那么仿站网就说说详细的步骤.首先,我们需要安装最新版本的phpcms V9其次,下载黄页模块,然后进行根目录的替换.再次, ...
- Android收起虚拟键盘
关于收起虚拟键盘,网上能找到的大多是这个: InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_M ...
- CRM认识的误区
众所周知,CRM,就是平时说的“客户关系管理”,指用CRM来管理 企业与客户之间的关系.纵观整个IT圈子,做CRM的厂商比比皆是,每个厂商都有自己的产品宣言,令人眼花缭乱.但是领很多老板们不解的是,我 ...