2020牛客国庆集训派对day1 A.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 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 ≤ 4e5 ), 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

马拉车算法~
马拉车算法的 l[i]l[i]l[i] 记录的是以 iii 为中心的最大回文子串半径,那么这题就是当 回文子串的末尾正好为字符串末尾时,去除回文子串,将字符串剩下的部分倒序添加在字符串末尾即可,答案也就是剩下部分的长度,AC代码如下:

#include<bits/stdc++.h>
using namespace std;
const int N=4e5+5;
int n;
char p[N];
char s[N<<1];
int l[N<<1];int change(char *p){int i,len=strlen(p);s[0]='@';for(int i=1;i<=2*len;i+=2){s[i]='#';s[i+1]=p[i>>1];}s[2*len+1]='#';s[2*len+2]='$';s[2*len+3]='\0';return 2*len+1;
}int manacher(char *s,int len){int mx=0,ans=len,pos=0;for(int i=1;i<=len;i++){if(mx>i) l[i]=min(mx-i,l[2*pos-i]);else l[i]=1;while(s[i-l[i]]==s[i+l[i]]) l[i]++;if(l[i]+i>mx){mx=l[i]+i;pos=i;}if(l[i]==len-i+1) ans=min(ans,(i-l[i]+1)/2);}return ans;
}int main(){scanf("%d%s",&n,p);int len=change(p);printf("%d",manacher(s,len));return 0;
}

2020牛客国庆集训派对day1 A.ABB相关推荐

  1. 2020牛客国庆集训派对day1 Zeldain Garden

    Zeldain Garden 题意: 问[L,R]内所有数的因子的数量和 题解: 如果传统暴力做肯定不行 我们来找找规律: 数字: 因子数目 1~n的因子数和 1 1 1 2 2 3=2+1/ 3 2 ...

  2. 2020牛客国庆集训派对day1 C. Bob in Wonderland

    Bob in Wonderland 题意: 一棵树,问最少移动多少次边可以使其变成一个链? 移动是指:从原位置拆下并连到新位置,这样算一次 题解: 错误思路 我一开始在想既然求最少移动次数,那我们就尽 ...

  3. ABB (2020牛客国庆集训派对day1)

    ABB 题意: 长度为n的字符串,问最少添加多少字符可以使其构成回文字符串 题解: 最长回文字符串我的第一反应是manacher马拉车算法,那我们直接马拉车找到已有最长回文串,然后总长度减去不就是答案 ...

  4. 2020牛客国庆集训派对day2 补题J

    2020牛客国庆集训派对day2 补题J:VIRUS OUTBREAK 题目描述 The State Veterinary Services Department recently reported ...

  5. 2020牛客国庆集训派对day3 I.Rooted Tree(哈代-拉马努金拆分数列)

    2020牛客国庆集训派对day3 I.Rooted Tree(哈代-拉马努金拆分数列) 题目 https://ac.nowcoder.com/acm/contest/7830/I 题意 给你n个点,问 ...

  6. 2020牛客国庆集训派对day2 H-STROOP EFFECT(英语题)

    2020牛客国庆集训派对day2 H-STROOP EFFECT(英语题) 题目 https://ac.nowcoder.com/acm/contest/7818/H 题意 这题目真的太难读懂了,赛后 ...

  7. 2020牛客国庆集训派对day8 G-Shuffle Cards(扩展STL容器,rope可持久化平衡树)

    2020牛客国庆集训派对day8 G-Shuffle Cards(扩展STL容器,rope可持久化平衡树) 题目 https://ac.nowcoder.com/acm/contest/7865/G ...

  8. (花里胡哨)New Game!(牛客国庆集训派对Day1)

    链接:https://ac.nowcoder.com/acm/contest/201/L 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言209 ...

  9. 2020牛客国庆集训派对day2 F题 Java大数处理

    题目: 链接:https://ac.nowcoder.com/acm/contest/16913/F 来源:牛客网 The following code snippet calculates the ...

最新文章

  1. MySQL Proxy实现数据库读写分离
  2. XSS挑战之旅闯关笔记
  3. php汽车找车位,车停在哪最省心?小编奉上找车位小指南
  4. 运行时错误7内存溢出_JVM运行时内存数据区域
  5. 怎么取消任意Windows窗口总在最前
  6. SpringBoot2.1.9 多Redis Jedis配置
  7. 在c语言中引用数组元素时,其数组下标的数据类型允许是什么
  8. 深度学习入门读书笔记:Day-14
  9. 商业智能SAAS走向中小企业
  10. 汇编程序:成绩分段统计
  11. opencv注释中的@brief、@code/@endcode、@param、@sa 以及 如何在pycharm中看到opencv函数的完整注释
  12. ORACLE常用函数汇总
  13. 杭州电子科技大学ACM注册与答题指南
  14. SEO优化师王永仙说:网页title设置及描述设置和URL优化及网站地图优化(sitemap)...
  15. png转jpg java_怎么把图片PNG格式转换成JPG格式?
  16. Android开发Tips(5)
  17. MongoDB的一些基本使用
  18. 解决希捷硬盘盒换硬盘后灯不亮
  19. 服务器sd卡装系统,sd卡如何重装系统操作教程
  20. 多项式的乘法满足结合律和交换律

热门文章

  1. 【Linux】关于权限的认识以及相关Linux权限热点面试题
  2. 0、‘0‘、‘\0‘的区分和联系
  3. Catlike Coding翻译——Movement 01移动球体(玩家控制)
  4. DELL远程控制卡 使用ipmitools设置ipmi
  5. 最大熵模型学习优化案例
  6. js 值引用和地址引用
  7. 达梦单机数据库安装(for linux)
  8. 【谷歌地图--集成准备】
  9. 酒鬼体育网-看体育赛事的最好网站
  10. scrapy 爬取起点中文网首页的本周强推作品的详情介绍