题目

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·10^5 ), 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.

样例

题目简化

给你一个长度为n的字符串,让你求出还需要在末尾添加多少个字符,可以使它变为回文串

思路

马拉车算法求出最大回文子串
注意:求回文子串的时候要加条件判断,是的最大回文子串的右端在原串中也是最右端

AC代码

//马拉车算法求最大回文子串长度
#include<stdio.h>
#include<algorithm>
using namespace std;
const int N = 4e5 + 15;
char str[N]; // 原字符串
char p[N * 2]; // 构造后的字符串
int len[N * 2];
int p_len; // 构造后字符串的长度
//构造字符串
void get_P(int n) {int t = 0;p[t++] = '@';for (int i = 0; i < n; i++) {p[t++] = '#';p[t++] = str[i];}p[t++] = '#';p_len = t;p[t] = '0';
}
int manache() {int R = 0, C; // R为最右边,C为中心点int res = 0;int res1 = -1;for (int i = 1; i < p_len; i++) {//当前在R范围之内,将关于中心点对称的左边的点的值赋值给此前点if (R > i)len[i] = min(R - i, len[2 * C - i]);elselen[i] = 1;//不断扩展while (p[i + len[i]] == p[i - len[i]])len[i]++;if (len[i] + i > R) {R = len[i] + i;C = i;}//更新最大值,也可以写在循环外面遍历寻找//若最大值对应的下标为x,则最大回文串的起始位置为(x - len[x]) / 2res = max(res, len[i]);//*///添加条件,只取回文串右端是原字符串最右端的回文串if (R == p_len)res1 = max(res1, res);res = 0;//*/}//返回最大回文子串的长度//return res - 1;//返回有条件的最大回文子串的长度return res1 - 1;
}
int main()
{int n; scanf("%d", &n);scanf("%s", str);get_P(n);int max_hui = manache();printf("%d\n", n - max_hui);return 0;
}

2020牛客国庆集训派对day1 ------ ABB(马拉车裸题 + 条件判断)相关推荐

  1. 2020牛客国庆集训派对day1 A.ABB

    2020牛客国庆集训派对day1 A.ABB 题目链接 题目描述 Fernando was hired by the University of Waterloo to finish a develo ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

最新文章

  1. [官方摘要]Setup And Configuration memcached with Tomcat
  2. asp.net 在 Ngnix 服务器 中配置攻略
  3. JVM GC一次调优实战
  4. linux gitlab 9 邮件不发送,gitlab无法发送邮件
  5. Deep learning:一(基础知识_1)
  6. 代码神注释鉴赏,喜欢拿去用
  7. 读取Android系统的多媒体库
  8. React开发(249):react项目理解 ant design input loading
  9. 【redis】redis应用场景,缓存的各种问题
  10. 售票pv操作java实现_随时随地打印手机照片,佳能瞬彩PV-123体验评测
  11. java求最小步数_关于java:查找两点之间的最小步数?
  12. Python中os.listdir和os.walk的区别
  13. AltiumDesigner06——常见晶振封装尺寸
  14. 用Python自动化爬取CNKI知网数据(批量下载PDF论文)
  15. 数据中心与灾备中心建设总结
  16. kaggle TalkingData用户性别数据预测性别入门笔记
  17. 2018-11-15-mqtt-mosquitto系列11之配置基于ca证书的桥接
  18. 如何给华硕笔记本在光驱位加装另一块linux系统固态硬盘?
  19. ios 开发 怎样在项目中使用除系统外的字体
  20. android声音大小锁定,固定音量锁(锁定音量)app

热门文章

  1. 解读品牌KOL运营之路
  2. pycharm官方下载库很慢、会失败的解决方法
  3. 阿里妈妈返利比率的商品搜索API接口
  4. IDEA debug或启动报错:maven-resources-production:XXX:java.lang.NegativeArraySizeException
  5. linux系统——窗口管理器和桌面环境的区别
  6. c语言编译器怎么防止优化变量,volatile关键字的作用:防止变量被编译器优化
  7. php和mysql关于ip段查询
  8. Android 10 后台启动app页面
  9. Codeforces 553A Kyoya and Colored Balls 给球涂颜色
  10. 华三交换机配置telnet远程登录和http、https登录