POJ-3974-Palindrome(马拉车)
链接:
http://poj.org/problem?id=3974
题意:
Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?"
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.
思路:
马拉车模板题.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e6+10;
int hw[MAXN*2], val[30], Sum[MAXN*2];
char s[MAXN], ss[MAXN*2];
void Manacher(char *str)
{
int maxr = 0, mid;
int len = strlen(str);
for (int i = 1;i < len;i++)
{
if (i < maxr)
hw[i] = min(hw[mid*2-i], maxr-i);
else
hw[i] = 1;
while (str[i+hw[i]] == str[i-hw[i]])
hw[i]++;
if (hw[i]+i > maxr)
{
maxr = hw[i]+i;
mid = i;
}
}
}
void Change(char *str, char *to)
{
to[0] = to[1] = '#';
int len = strlen(str);
for (int i = 0;i < len;i++)
{
to[i*2+2] = str[i];
to[i*2+3] = '#';
}
to[len*2+2] = 0;
}
int main()
{
int cnt = 0;
while (~scanf("%s", s))
{
if (s[0] == 'E')
break;
Change(s, ss);
Manacher(ss);
int ans = 0;
int len = strlen(ss);
for (int i = 0;i < len;i++)
ans = max(ans, hw[i]);
printf("Case %d: %d\n", ++cnt, ans-1);
}
return 0;
}
原文地址:https://www.cnblogs.com/YDDDD/p/11605378.html
- 国内自动驾驶企业在美国首秀其最新L4级自动驾驶货运卡车
- Nginx源码安装及调优配置
- Greenplum 简单性能测试与分析
- CentOS6 安装couchdb2 集群
- 如何遍历维数和各维上限未定的多维数组
- 求长度的另一种方法(""+obj).Length
- ASP.NET MVC的客户端验证:jQuery验证在Model验证中的实现
- GreenPlum 简单性能测试与分析(续)
- 最终版 Reflector v1.0 (+简单的反流程混淆)
- 性能&分布式&NewLife.XCode对无限数据的支持
- ASP.NET MVC下的异步Action的定义和执行原理
- 包学会之浅入浅出Vue.js:结业篇
- 迈克尔•戴尔:人工智能杀手?技术反乌托邦?不存在的
- 你知道吗?多个类多线程环境下静态构造函数的执行顺序
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android Studio手动配置Gradle的方法
- Android仿微信@好友功能 输入@跳转、删除整块
- Android开发实现广告无限循环功能示例
- Android仿微信底部菜单栏效果
- MySQL 案例:Update 死锁详解
- Android 线程之自定义带消息循环Looper的实例
- 详解Androidstudio3.0 关于Gradle报错的问题(小结)
- Android开发实现文件关联方法介绍
- Android开发获取重力加速度和磁场强度的方法
- Android自定义View圆形和拖动圆跟随手指拖动
- 简单好用的Adapter—ArrayAdapter详解
- Android开发之图片切割工具类定义与用法示例
- Android开发之超强图片工具类BitmapUtil完整实例
- Android使用URL读取网络资源的方法
- Android控件RecyclerView实现混排效果仿网易云音乐