回文串---Palindrome
Description
A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.
The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!".
If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.
Input
Output
Sample Input
abcbabcbabcba
abacacbaaaab
END
Sample Output
Case 1: 13
Case 2: 6 题意:给一个字符串求这个串的最长回文子串的长度; 思路:用以下回文串的模板可以在线性时间内完成求最长的回文串的长度;
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=;
int n,p[N];
char s[N],str[N]; void kp()
{
int i;
int mx=;
int id;
///for(i=n;str[i]!=0;i++)
///str[i]=0; ///没有这一句有问题,就过不了ural1297,比如数据:ababa aba;
for(i=;i<n;i++)
{
if(mx>i)
p[i]=min(p[*id-i],p[id]+id-i);
else
p[i]=;
for( ;str[i+p[i]]==str[i-p[i]];p[i]++);
if(p[i]+i>mx)
{
mx=p[i]+i;
id=i;
}
}
} void init()
{
str[]='$';
str[]='#';
for(int i=;i<n;i++)
{
str[i*+]=s[i];
str[i*+]='#';
}
n=n*+;
s[n]=;
} int main()
{
int Case=;
while(scanf("%s",s)!=EOF)
{
if(s[]=='E'&&s[]=='N'&&s[]=='D')
break;
n=strlen(s);
init();
kp();
int ans=;
for(int i=;i<n;i++)
if(p[i]>ans)
ans=p[i]; printf("Case %d: %d\n",Case++,ans-);
}
return ;
}
回文串---Palindrome的更多相关文章
- [Swift]LeetCode131. 分割回文串 | Palindrome Partitioning
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- 分割回文串 · Palindrome Partitioning
[抄题]: 给定一个字符串s,将s分割成一些子串,使每个子串都是回文串. 返回s所有可能的回文串分割方案. 给出 s = "aab",返回 [ ["aa", & ...
- LeetCode 131. 分割回文串(Palindrome Partitioning)
131. 分割回文串 131. Palindrome Partitioning 题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. LeetC ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
- hdu 1159 Palindrome(回文串) 动态规划
题意:输入一个字符串,至少插入几个字符可以变成回文串(左右对称的字符串) 分析:f[x][y]代表x与y个字符间至少插入f[x][y]个字符可以变成回文串,可以利用动态规划的思想,求解 状态转化方程: ...
随机推荐
- ViewHolder模式超简洁写法
ViewHolder是什么就不解释了.大家通常怎么写ViewHolder呢? ViewHolder holder = null; if (convertView == null) { convertV ...
- Cocos2d-JS项目之二:studio基础控件的使用
在studio里把几个基础控件往场景文件一拖,然后导出json格式的资源文件 逻辑代码如下: var HelloWorldLayer = cc.Layer.extend({ sprite:null, ...
- elasticsearch + hive环境搭建
一.环境介绍: elasticsearch:2.3.1 hive:0.12 二.环境搭建 2.1 首先获取elasticsearc-hadoop的jar包 链接地址:http://jcenter.bi ...
- saiku、mondrian前奏之——立方体、维度、Schema的基本概念
以前介绍了几个基本工具:saiku 和 Schema Workbench,算是入门级别的了解多维报表,如果要继续深入,需要深入了解如下几个概念: 1.OLAP 联机分析处理,和他对应的是OLTP(联机 ...
- 在Linux 应用层 基于i2c-dev.h 实现i2c读写
/* i2c-dev.h - i2c-bus driver, char device interface Copyright (C) 1995-97 Simon G. Vogl Copyright ( ...
- c++ 职责链模式(Chain of Responsibility)
职 责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处 理它为止.其思想很简单,考虑员工要求加薪.公司的管 ...
- python 字符串翻转
通过步进反转[::-1] ]##[::-1]通过步进反转print b
- 自定义组件之MoreListView
前言 本文针对自定义组件进行一些分析.还是那句老话“授之于鱼不如授之以渔”.今天要讲的是一个自定义的可以分页的ListView. 网上都讲了些ListView分页的方法,那么为什么我在这里还需要自己写 ...
- Heavy Transportation(最短路 + dp)
Heavy Transportation Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64 ...
- 【转】ContextMenuStrip菜单应用
测试可用的代码: #region 右键快捷菜单单击事件 private void contextMenuStrip1_ItemClick(object sender, EventArgs e) { T ...