ABB 2020牛客国庆集训派对day1(Manacher模板题)

ABB

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

  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 in various 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 counterbalance the chaos of all bungalow shapes, he wants to add some order to the arrangement by choosing suitable colors for the new bungalows. When the project is finished, the whole sequence of bungalow colors will be symmetric, that is, the sequence of colors is the same when observed from either end of the street.
  Among other questions, Fernando wonders what is the minimum number of new bungalows he needs to build and paint appropriately to complete the project while respecting his self-imposed bungalow color constraint.

输入描述:

  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 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

示例2

输入

12
recakjenecep

输出

11

示例3

输入

15
murderforajarof

输出

6

代码:

#include<iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <algorithm>
#define ll int
#define PI acos(-1)
#define mes(x,y) memset(x,y,sizeof(x))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const ll inf = 2e9 + 10;
const ll mod = 1e9 + 7;
ll n, m, i, j, k = 0, t, w, flag, sum;
string s;

const int MAXN = 4e5 + 10;

ll Mp[MAXN * 2] = { 0 };

string Ma;

ll Manacher() {

	for (Ma = "$#", i = 0; i < n; i++)Ma += s[i], Ma += "#";

	for (ll i = 0, mx = 0, id = 0, l = Ma.length(); i < l; i++) {

		Mp[i] = mx > i ? min(Mp[2 * id - i], mx - i) : 1;

		while (Ma[i + Mp[i]] == Ma[i - Mp[i]])Mp[i]++;

		if (i + Mp[i] >= mx) { 

			if (i + Mp[i] == l)return n - Mp[i] + 1;

			mx = Mp[i] + i; 

			id = i; 

		}

	}

}

int main() {

	cin >> n >> s;

    cout << Manacher() << endl;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

GUESSERR

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

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

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

打赏作者

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

抵扣说明:

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

余额充值