ABB(manacher)

题目连接: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;
} 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,没有找到关于"ABB UIDnumEntry"的具体信息。可能是因为该条目未被提及或未包含在提供的引用内容中。如果您需要关于"ABB UIDnumEntry"的信息,请提供更多的背景或详细的描述,以便我能更好地帮助您。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [ABB机器人资料ABB机器人基础及培训手册ABB机器人编程及指令手册.zip](https://download.csdn.net/download/SKCQTGZX/21785512)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [最全的ABB机器人手册资料汇总.zip](https://download.csdn.net/download/goodxianping/44914428)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [abb码垛取料仿真工作站_RobotStudio_robotstudio仿真_abb码垛仿真_abb码垛_ABB机器人_](https://download.csdn.net/download/weixin_42666036/25781756)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值