A-ABB(计蒜客 - 44953)

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

题意:在给定的字符串后添加多少个字符后可以让该字符串变为回文字符串。
思路:以当前最中间的的字符作为(假定)回文串的中心,检测是不是回文串,不是则(假定)回文串中心后移一个位置。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[400005];
int main() 
{
int n;
cin >> n;
for(int i=1;i<=n;i++)
	cin>>s[i];
int sum=0;
for(int i=1;i<=n;i++)
{
	sum=0;
	for(int j=0;j<=(n-i+2)/2;j++)
	{
		if(i+j<n-j&&s[i+j]==s[n-j])
		sum+=2;
		else if(i+j==n-j)sum++;
		else break;
	}
	if(sum==n-i+1){
	printf("%d\n",i-1);
	return 0;}
} 
printf("%d\n",n-1);
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值