题目连接: 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;
}
温习一下马拉车. 可能不是这个題的什么优质解.