hiho-最长回文子串

//  题目连接:http://hihocoder.com/problemset/problem/1032
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
#define N 2000010

char f[N];
int deal(){
	int ans = 1;
	int len = strlen(f);
	for(int i=0;i<len;i++){
		int s,e;s = i;e = i;
		int t;
		while(f[e+1]==f[i]) e++;
		i = e;
		while(f[s-1]==f[e+1] && s-1>=0) {
			s--;e++;
		}
		if((t = (e - s + 1)) > ans) ans = t;
	}
	return ans;
}
int main() {
	//freopen("in.in","r",stdin);
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%s",f);
		printf("%d\n",deal());
	}

	return 0;
}


·一个挺好的思路:枚举每个子串中间点,向两边扩展,看是否是回文子串。

·注意子串为偶的时候,这里使用while(f[e+1]==f[i]) e++; 来处理。


·这道题的重点在 deal() 函数中的 i = e;这句话,这句话使得   abaaaaaaaaaababa这种情况,中间相同a的部分只需要计算一次,大大减少了特殊情况下的枚举数量


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值