ABB(马拉车)

题目连接: 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 invarious 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 counter balance the chaosof all bungalow shapes, he wants to add some order to the arrangement by choosing suitablecolors for the new bungalows. When the project is finished, the whole sequence of bungalowcolors will be symmetric, that is, the sequence of colors is the same when observed from eitherend of the street.

Among other questions, Fernando wonders what is the minimum number of new bungalows heneeds to build and paint appropriately to complete the project while respecting his self-imposed bungalow color constraint.

Input Specification

The first line contains one integer N(1≤N≤4⋅105), 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 Specification

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.

Sample Input
3
abb

Sample Output
1

Sample Input 2
12
recakjenecep

Sample Output 2
11

大致题意:

给你一个由小写字母组成的字符串, 并告诉你其长度, 问你最少还得补多少个字符能让该串变为回文串.

解题思路:

一提到字符串的回文串, 理所当然就想到了马拉车算法, 因此本题我采用马拉车算法的思路.

首先将子串进行一次标准型转化, 转化成形如 @X#X#…X@的形式, 其中’@'表示串的起始与结束, 'X’为小写字母, '#'为特殊填充符. 我们称其为标准串. 对于标准串, 我们会发现其一定为一个奇数串.

对于字符串对称的形式: 一种是形如ABBA式, 对应标准串中以’#'为回文中心的形式. 而另一种是形如ABA式, 对应标准串中以’X’为回文中心的字符串.

我们通过观察会发现, 最理想的情况为该串本身即为回文串, 即对称中心在串长的一半处. 如果不是, 则补成回文串后, 对称中心会右移. 所以我们只需要依次枚举对称中心即可.

如果找到了对称中心, 我们可以发现无论是哪种对称形式, 所需要补充的字符数字都为下标/2.

AC代码:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MAXN = 1E6; //注意标准串转化时, 长度会翻倍.
char t[MAXN], s[MAXN]; int n;
void init() {
	cin >> n >> t;
	for (int i = 0; i < n; ++i) {
		s[i * 2] = '#';
		s[i * 2 + 1] = t[i];
	}
	s[0] = '@'; s[strlen(s)] = '@'; s[strlen(s) + 1] = '\0';
}
void fact() {
	int mid = n; 
	while (mid < strlen(s)) {
		char* i = &s[mid - 1];
		char* j = &s[mid + 1];
		while (true) {
			if (*j == '@') { //我们的mid是从中间往右挪, 若要结束, 左侧不会在右侧之前遇到'@'
				const int temp = i - &s[0];
				cout << temp / 2 << endl;
				return;
			}
			if (*i != *j) break;
			i--, j++;
		}
		mid++;
	}
}
int main(void)
{
	init(); //初始化操作, 通过该函数得到标准串@X#X#...X@
	fact(); 
	return 0;
}

温习一下马拉车. 可能不是这个題的什么优质解.

END

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逍遥Fau

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值