【HDU - 3068】最长回文(Manacher算法,马拉车算法求最长回文子串)

题干:

给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 
回文就是正反读都是一样的字符串,如aba, abba等

Input

输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c...y,z组成的字符串S 
两组case之间由空行隔开(该空行不用处理) 
字符串长度len <= 110000

Output

每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度. 

Sample Input

aaaaabab

Sample Output

4
3

解题报告:

    裸的马拉车算法,一定注意修改后的数组需要开二倍就好了,,,不然会RE但是我也不知道为什么HDUoj报TLE很迷。。但是这题好像可以暴力啊。。跑出来都是200多ms。。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int top;
char s[110005],str[110005<<1];
int p[120005<<1];
int manacher() {if(top == 0) return 0 ;int maxx = -1;int c = -1,r = -1;for(int i = 1; i<top; i++) {p[i] = r>i ? min(p[2*c-i],r-i) : 1;
//		while(i+p[i]<top && i-p[i] > -1) {
//			if(str[i+p[i]] == str[i-p[i]]) p[i]++;
//			else break;			
//		}for(;str[i-p[i]] == str[i+p[i]];p[i]++);if(i+p[i] > r) {r = i+p[i];c=i;}maxx = max(maxx,p[i]);}return maxx-1;
}
int main()
{while(~scanf("%s",s)) {int len = strlen(s);top=0;str[top++] = '@';for(int i = 0; i<len; i++) {str[top++] = '#';str[top++] = s[i];}str[top++] = '#';str[top] = '\0';
//		printf("%s\n",str);printf("%d\n",manacher());} return 0 ;}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441351.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java程序员面试需要英语吗_Java程序员和高级程序员面试30题(英语)

Java程序员和高级程序员面试30题(英语)* Q1. How could Java classes direct program messages to the system console, but error messages, say to a file?A. The class System has a variable out that represents the standard output, and the variable err that represe…

【HDU - 1867 】A + B for you again(KMP,next数组应用)

题干&#xff1a; Generally speaking, there are a lot of problems about strings processing. Now you encounter another such problem. If you get two strings, such as “asdf” and “sdfg”, the result of the addition between them is “asdfg”, for “sdf” is …

wifisetting.java_Wifi 笔记 | 启动流程

csd&#xff1a;csdn_of_coder/article/details/51541094aosp: Android OAndroid网络各个模式中&#xff0c;Wifi应该是目前最常用的一种网络方式了&#xff1b;下面就简单介绍下Android中Wifi的启动流程。当我在Setting菜单里点击打开Wifi时&#xff0c;调用的入口函数是WifiM…

【CodeForces - 589F】Gourmet and Banquet (贪心,思维,二分)

题干&#xff1a; A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet knows the schedule: when each of the dishes will be served. For i-th of the dishes he knows two integer moments in time ai and bi (in second…

java 调用动态链接库_JAVA技巧:JNative调用动态链接库问题(SOS)

动态链接库的方法如下&#xff1a;__declspec(dllexport) ret __stdcall rLachTran(const char *pc_trancode,const char *pc_clicode,const char *pc_orgcode,const char *pc_ttycode,const int i_brandid,const char *pc_reqstamp,const int i_reqseqno,const char *pc_svrip…

SPFA算法模板

简单的一个模板吧&#xff0c;留个底。如果要判负环的话&#xff0c;需要加一个cnt数组记录入队次数就可以了。 void spfa(int st) {memset(dis,INF,sizeof dis);queue<int> q;q.push(st);dis[st]0;vis[st]1;while(!q.empty()) {int cur q.front();q.pop();vis[cur]0;i…

js和php能生成一样的随机数_JavaScript_JS生成某个范围的随机数【四种情况详解】,前言: JS没有现成的函数,能 - phpStudy...

JS生成某个范围的随机数【四种情况详解】前言&#xff1a;JS没有现成的函数&#xff0c;能够直接生成指定范围的随机数。但是它有个函数&#xff1a;Math.random() 这个函数可以生成 [0,1) 的一个随机数。利用它&#xff0c;我们就可以生成指定范围内的随机数。而涉及范围的话…

【POJ - 3347 】Kadj Squares (计算几何,思维 或 扫描线)

题干&#xff1a; In this problem, you are given a sequence S1, S2, ..., Sn of squares of different sizes. The sides of the squares are integer numbers. We locate the squares on the positive x-y quarter of the plane, such that their sides make 45 degrees w…

按钮开关java代码,Android自定义实现开关按钮代码

我们在应用中经常看到一些选择开关状态的配置文件&#xff0c;做项目的时候用的是android的Switch控件&#xff0c;但是感觉好丑的样子子个人认为还是自定义的比较好&#xff0c;先上个效果图&#xff1a;实现过程&#xff1a;1.准备开关不同状态的两张图片放入drawable中。2.x…

java创建的zip没写入权限,java中的zip创建错误

我正在使用ZipOutputStream,FileOutputStream和FileInputStream.首先我创建了一个包含一个文件的文件夹它成功创建了.然后我尝试创建zip文件.动态地,它首次正确创建文件,但第二次,第三次在打开文件时出错.Error: zip [path/././file.zip] Cannot open The process cannot acces…

【qduoj - 夏季学期创新题】最长公共子串(水题暴力枚举,不是LCS啊)

题干&#xff1a; 描述 编写一个程序&#xff0c;求两个字符串的最长公共子串。输出两个字符串的长度&#xff0c;输出他们的最长公共子串及子串长度。如果有多个最长公共子串请输出在第一个字符串中先出现的那一个。 特别注意公共子串中可能包含有空格&#xff0c;但不计回车…

向量合并 matlab,MATLAB追加向量

如果有两个行向量 r1 和 r2 这两个行向量中各有 n 和 m 个元素&#xff0c;现在创建行向量 r 并将n和m个元素都放在行向量 r 中&#xff0c;通过附加这些载体&#xff0c;编写&#xff1a;r [r1,r2]通过追加这两个向量&#xff0c;向量r2的&#xff0c;也可以建立一个矩阵R&am…

*【CodeForces - 202C 】Clear Symmetry (思维,找规律,特判)

题干&#xff1a; Consider some square matrix A with side n consisting of zeros and ones. There are nrows numbered from 1 to n from top to bottom and n columns numbered from 1 to n from left to right in this matrix. Well denote the element of the matrix wh…

matlab将模型解封装,模型保护 - MATLAB Simulink - MathWorks 中国

当您要与第三方共享模型而又不能泄露知识产权时&#xff0c;请对模型进行保护。Test your protected model by comparing it to the original model.Attach a digital signature to your protected model.Files to include in the protected model package.Specify a post-proc…

*【CodeForces - 768B】Code For 1 (分治策略,模拟二分思想,模拟线段树思想)

题干&#xff1a; Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that he wants to go to Oldtown to train at the Citadel to become a maester, so he can return and take the decease…

matlab 自适应噪声对消,基于Matlab的RLS自适应语音噪声对消系统的设计与实现

基于Matlab 的R LS 自适应语音噪声对消系统的设计与实现①肖 哲(湖南工业大学科技学院, 湖南株洲 412008)摘 要:自适应信号处理的理论和技术经过40多年的发展和完善,已逐渐成为人们常用的语音去噪技术.而Matlab 的出现又为其提供了更为方便快捷的方法来对语音信号进行消噪处…

【qduoj - 142】 多重背包(0-1背包的另类处理,dp)

题干&#xff1a; ycb的ACM进阶之路 Description ycb是个天资聪颖的孩子&#xff0c;他的梦想是成为世界上最伟大的ACMer。为此&#xff0c;他想拜附近最有威望的dalao为师。dalao为了判断他的资质&#xff0c;给他出了一个难题。dalao把他带到一个到处都是题的oj里对他说&am…

python数字类型怎么学,python的数字类型学习之数据类型

1、在python中&#xff0c;数字并不是一个真正的对象类型&#xff0c;而是一组类似类型的分类。它支持通常的数字类型&#xff0c;还能够可以通过常量直接创建数字&#xff0c;还可以处理数字表达式。2、数字常量&#xff1a;(1)整数和浮点数常量(2)16进制、8进制、2进制常量(3…

贪心算法 -- 最小延迟调度

转自&#xff1a;https://blog.csdn.net/bqw18744018044/article/details/80285414 总结&#xff1a; 首先&#xff0c;证明贪心的时候交换论证是万能的&#xff01;其次&#xff0c;这一点如果要满足&#xff0c;也就是&#xff0c;如果你要用交换论证法&#xff0c;那么首先…

php成行排列,一个php实现的生成排列的算法

function perm($s, $n, $index){if($n 0){return ;}else{$nIndex count($index); //可用的字符串下标$res array();foreach($index as $i > $v){$tmp $index;unset($tmp[$i]); //去掉当前的前缀/* 调试信息&#xff0c;便于理解echo "len $n , cur $i , index:\n&q…