题目连接:https://nanti.jisuanke.com/t/44953
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 \leq N \leq 4·10^{5})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.
样例输入1复制
3 abb
样例输出1复制
1
样例输入2复制
12 recakjenecep
样例输出2复制
11
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 \leq N \leq 4·10^{5})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.
样例输入1复制
3 abb
样例输出1复制
1
样例输入2复制
12 recakjenecep
样例输出2复制
11
思路: 贪心,从左向右枚举字符串(i表示下标),求以i为中心的最大回文字串的长度,暴力的话O(n^2)超时了,需要用到O(n)的manacher来做,但是这个题的数据貌很水,暴力也可以过(需要注意偶回文串还是奇回文串)。manacher的话就是遍历这个构建的新数组,如果(i-p[i])/2+p[i]-1等于原字符串的长度时就可以输出答案了。
code:
#include<bits/stdc++.h>
using namespace std;
int manacher(string s){
int len=s.length();
string str="";
str+="$#";
for(int i=0;i<len;i++){
str+=s[i];
str+="#";
}
vector<int> p(str.size(),0);//vector是不能像数组一样直接赋值的
int mx=0,id=0;
for(int i=1;i<str.size();i++){//计算p数组
if(i<mx){
p[i]=min(p[2*id-i],mx-i);
}
else p[i]=1;
while(str[i+p[i]]==str[i-p[i]]) p[i]++;
if(i+p[i]>mx){
mx=i+p[i];
id=i;
}
}
int ans=0;
for(int i=2;i<str.length();i++){
if(p[i]==1) continue;
if((i-p[i])/2+p[i]-1==len){//这里注意不能通过构建的新串的mid求原串的mid的,因为原串可能是偶串,没有mid
ans=(i-p[i])/2;//(i-p[i])/2为原串子回文串的起点的下标 p[i]-1为子回文串的长度, (i-p[i])/2+p[i]-2为终点的下标
break;
}
}
return ans;
}
int main()
{
string s;
int n;
cin>>n;
cin>>s;
cout<<manacher(s)<<endl;
return 0;
}