2020牛客国庆集训派对day1 ------ ABB(马拉车裸题 + 条件判断)

题目

Fernando was hired by the University of Waterloo to finish a development project the university started some time ago. Outside the campus, the university wanted to build its representative bungalow street for important foreign visitors and collaborators.
Currently, the street is built only partially, it begins at the lake shore and continues into the forests, where it currently ends. Fernando’s task is to complete the street at its forest end by building more bungalows there. All existing bungalows stand on one side of the street and the new ones should be built on the same side. The bungalows are of various types and painted in various colors.
The whole disposition of the street looks a bit chaotic to Fernando. He is afraid that it will look even more chaotic when he adds new bungalows of his own design. To counterbalance the chaos of all bungalow shapes, he wants to add some order to the arrangement by choosing suitable colors for the new bungalows. When the project is finished, the whole sequence of bungalow colors will be symmetric, that is, the sequence of colors is the same when observed from either end of the street.
Among other questions, Fernando wonders what is the minimum number of new bungalows he needs to build and paint appropriately to complete the project while respecting his self-imposed bungalow color constraint.

输入

The first line contains one integer N (1 ≤ N ≤ 4·10^5 ), the number of existing bungalows in the street. The next line describes the sequence of colors of the existing bungalows, from the beginning of the street at the lake. The line contains one string composed of N lowercase letters (“a” through “z”), where different letters represent different colors.

输出

Output the minimum number of bungalows which must be added to the forest end of the street and painted appropriately to satisfy Fernando’s color symmetry demand.

样例

在这里插入图片描述

题目简化

给你一个长度为n的字符串,让你求出还需要在末尾添加多少个字符,可以使它变为回文串

思路

马拉车算法求出最大回文子串
注意:求回文子串的时候要加条件判断,是的最大回文子串的右端在原串中也是最右端

AC代码

//马拉车算法求最大回文子串长度
#include<stdio.h>
#include<algorithm>
using namespace std;
const int N = 4e5 + 15;
char str[N]; // 原字符串
char p[N * 2]; // 构造后的字符串
int len[N * 2];
int p_len; // 构造后字符串的长度
//构造字符串
void get_P(int n) {
	int t = 0;
	p[t++] = '@';
	for (int i = 0; i < n; i++) {
		p[t++] = '#';
		p[t++] = str[i];
	}
	p[t++] = '#';
	p_len = t;
	p[t] = '0';
}
int manache() {
	int R = 0, C; // R为最右边,C为中心点
	int res = 0;
	int res1 = -1;
	for (int i = 1; i < p_len; i++) {
		//当前在R范围之内,将关于中心点对称的左边的点的值赋值给此前点
		if (R > i)
			len[i] = min(R - i, len[2 * C - i]);
		else
			len[i] = 1;
		//不断扩展
		while (p[i + len[i]] == p[i - len[i]])
			len[i]++;
		if (len[i] + i > R) {
			R = len[i] + i;
 			C = i;
		}
		//更新最大值,也可以写在循环外面遍历寻找
		//若最大值对应的下标为x,则最大回文串的起始位置为(x - len[x]) / 2
		res = max(res, len[i]);
		//*///添加条件,只取回文串右端是原字符串最右端的回文串
		if (R == p_len)
			res1 = max(res1, res);
		res = 0;
		//*/
	}
	//返回最大回文子串的长度
	//return res - 1;
	//返回有条件的最大回文子串的长度
	return res1 - 1;
}
int main()
{
	int n; scanf("%d", &n);
	scanf("%s", str);
	get_P(n);
	int max_hui = manache();
	printf("%d\n", n - max_hui);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值