【HDU - 3746 】Cyclic Nacklace (KMP,最小循环节问题)

题干:

CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspired by the entrepreneurial spirit of "HDU CakeMan", he wants to sell some little things to make money. Of course, this is not an easy task. 

As Christmas is around the corner, Boys are busy in choosing christmas presents to send to their girlfriends. It is believed that chain bracelet is a good choice. However, Things are not always so simple, as is known to everyone, girl's fond of the colorful decoration to make bracelet appears vivid and lively, meanwhile they want to display their mature side as college students. after CC understands the girls demands, he intends to sell the chain bracelet called CharmBracelet. The CharmBracelet is made up with colorful pearls to show girls' lively, and the most important thing is that it must be connected by a cyclic chain which means the color of pearls are cyclic connected from the left to right. And the cyclic count must be more than one. If you connect the leftmost pearl and the rightmost pearl of such chain, you can make a CharmBracelet. Just like the pictrue below, this CharmBracelet's cycle is 9 and its cyclic count is 2: 


Now CC has brought in some ordinary bracelet chains, he wants to buy minimum number of pearls to make CharmBracelets so that he can save more money. but when remaking the bracelet, he can only add color pearls to the left end and right end of the chain, that is to say, adding to the middle is forbidden. 
CC is satisfied with his ideas and ask you for help.

Input

The first line of the input is a single integer T ( 0 < T <= 100 ) which means the number of test cases. 
Each test case contains only one line describe the original ordinary chain to be remade. Each character in the string stands for one pearl and there are 26 kinds of pearls being described by 'a' ~'z' characters. The length of the string Len: ( 3 <= Len <= 100000 ).

Output

For each case, you are required to output the minimum count of pearls added to make a CharmBracelet.

Sample Input

3
aaa
abca
abcde

Sample Output

0
2
5

题目大意:

  给定一个字符串,问至少还需要在末尾添加多少个字符才能构成一个带有循环节的字符串。

解题报告:

  直接KMP,归为最小循环节问题。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
char s[MAX];
int Next[MAX];
void getnext() {Next[0] = -1;int k = -1,j = 0;int len = strlen(s); while(j < len) {if(k == -1 || s[k] == s[j]) {k++,j++;Next[j] = k;}else k = Next[k];}
}
int main()
{int t;cin>>t;while(t--) {scanf("%s",s);getnext();int len = strlen(s);int tmplen = strlen(s) - Next[len];if(len != tmplen && len % tmplen == 0) puts("0");else printf("%d\n",tmplen - Next[len]%tmplen);} return 0 ;}

总结:

  注意输出0的时候需要if掉循环节长度  不等于  字符串长度,,不然就得直接输出字符串长度(但是可以归类到else那个情况中)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440886.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

autosar网络管理_AP AUTOSAR平台设计(11)——网络管理

点击蓝字右上角 关注置顶不迷路Hello&#xff01;大家好&#xff01;欢迎来到《搞一下汽车电子》本篇是AP AUTOSAR平台设计(11)——网络管理如果觉得不错&#xff0c;“转发” “ 在看”支持一下吧~1.网络管理算法概述AUTOSAR NM基于分散的网络管理策略&#xff0c;这意味…

【牛客 - 331G】炫酷数字(反素数打表 或 扩展埃式筛法,结论)

题干&#xff1a; 小希希望你构造一个最小的正整数&#xff0c;使得其有n个因子。 输入描述: 第一行一个整数T表示数据组数每组数据第一行输入一个正整数n&#xff0c;表示其因子数。n≤1,000,000n≤1,000,000T≤1,000,000T≤1,000,000输出描述: 输出一行一个整数&#xff…

【LeetCode - 279】完全平方数(四平方和定理,数论,图论,dp)

题干&#xff1a; 给定正整数 n&#xff0c;找到若干个完全平方数&#xff08;比如 1, 4, 9, 16, ...&#xff09;使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。 示例 1: 输入: n 12 输出: 3 解释: 12 4 4 4. 示例 2: 输入: n 13 输出: 2 解释: 13 …

nutch mysql solr_Nutch2.1+mysql+solr3.6.1+中文网站抓取

1、mysql 数据库配置 linux mysql安装步骤省略。 创建数据库与表 [sql] view plaincopyprint? CREATE DATABASE nutch DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci; CREATE TABLE webpage( id varchar (767) CHARACTER SET latin1 NOT N1、mysql 数据库配置l…

centos web 访问mysql_Centos7安装Web服务器--Mysql5.7.12安装

我是勤劳的搬运工……源码装了好多次Mysql&#xff0c;终于成功了……因为安装mysql需要对应的boost文件(必须是相应版本&#xff0c;不能高也不能低)&#xff0c;所以&#xff0c;为了不再麻烦的搜索对应的boost文件&#xff0c;下载包含boost的Mysql包2.查看是否有编译器# gc…

【牛客 - 368C】流星雨(概率dp,乘法逆元)

题干&#xff1a; 现在一共有n天&#xff0c;第i天如果有流星雨的话&#xff0c;会有wiwi颗流星雨。 第i天有流星雨的概率是pipi。 如果第一天有流星雨了&#xff0c;那么第二天有流星雨的可能性是p2Pp2P&#xff0c;否则是p2p2。相应的&#xff0c;如果第i−1 (i≥2)i−1 (…

mysql workbench 无法编辑_MySQL Workbench编辑表数据是只读的

14 个答案:答案 0 :(得分&#xff1a;60)我假设桌子有一把主键。首先尝试运行unlock tables命令以查看是否修复了它。如果所有其他方法都失败了&#xff0c;您可以更改表以创建具有自动增量的新主键列&#xff0c;并希望能够修复它。一旦完成&#xff0c;您应该能够毫无问题地删…

【计蒜客 - 蓝桥训练】炮台实验(数学期望,期望dp)

题干&#xff1a; 蒜头君在玩一个战争模拟游戏&#xff0c;他有高度为 1,2,3,\ldots ,n1,2,3,…,n 的炮台各一个&#xff0c;他需要把这 nn 个炮台从左往右排成一行&#xff0c;并且炮口都朝向右边。 在这个游戏中&#xff0c;所有炮台发射的炮弹会摧毁前方所有高度比自己低的…

mysql本身主从_Mysql主从复制

Mysql环境准备#下载Mysqlwget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm如果报错: -bash: wget: 未找到命令安装插件 yum -y install wget安装mysql-community-release-el7-5.noarch.rpmsudo rpm -ivh mysql-community-release-el7-5.noarch.rpm安装Mys…

phison主控ps3111量产工具_从固态硬盘拆解看门道 深入解读闪存编号和主控容量...

一篇固态硬盘的评测是否有水平&#xff0c;不光是看测试跑分是否详细&#xff0c;更应有针对成绩的解读以及硬件拆解和结构上的分析。毕竟跑分大家都会跑&#xff0c;必须拿出其他人不知道的内存才能真正吸引读者。因为大多数固态硬盘拆解会失去保修&#xff0c;所以拆解是网友…

alot英文怎么读_【乐学】“原来如此”用英文怎么说?

英语中的“原来如此”则有几种不同的表达下面就让我们一起来看看吧&#xff01;I seeI see 作为最地道的表达&#xff0c;使用起来准没错。这是来自《柯林斯词典》的释义&#xff1a;You can say "I see" to indicate that you understand what someone is telling y…

特殊mac地址文档_Mac颜值秒杀Win10?五款神器让Win10化身macOS

微软在Win10上捣鼓已经不是一天两天了&#xff0c;可结果……还是难以令人满意&#xff0c;特别是不伦不类的UI&#xff01;其实Win10的底子还是不错的&#xff0c;新技术用了不少&#xff0c;外观也比之前更有颜值&#xff0c;但……总感觉缺了点什么&#xff01;有没有羡慕隔…

checkbox设置三种状态 qt_checkbox的三种状态处理

checkbox只有两种值&#xff1a;选中(checked)或未选中(unchecked)。它可以有任何值&#xff0c;但是表单提交时checkbox的值只能是checked或unchecked。它的默认值是unchecked&#xff0c;你可以在HTML中这样控制它&#xff1a;视觉上&#xff0c;checkbox有三种状态&#xff…

raft算法mysql主从复制_Etcd raft算法实现原理分析

1.1 主要概念要实现集群数据的一致性&#xff0c;节点在进行通信的时候必定需要遵守特定规则进行数据校验&#xff0c;而这些规则具体都是通过某些具有特定含义的属性来实现的。为了让对Raft 算法比较陌生的读者对算法的关键概念有一个初步认识&#xff0c;作者整理了算法中涉及…

【面试题 - 最大值减去最小值小于或等于 num 的子数组数量】滑动窗口

题干&#xff1a; 解题报告&#xff1a; 我们用两个指针&#xff08;i&#xff0c;j&#xff09;分别代表窗口的左边界和右边界&#xff0c;窗口也就是子数组&#xff1b; 用两个双端队列分别维护这个窗口的最大值和最小值&#xff1b; 当窗口扩大时&#xff0c;即j向右扩展时…

mysql根据用户名查询数据_MySQL 查询数据

MySQL 查询数据 MySQL 数据库使用SQL SELECT语句来查询数据。 你可以通过 mysql> 命令提示窗口中在数据库中查询数据,或者通过PHP脚本来查询数据。 语法 以下为在MySQL数据库中查询数据通用的 SELECT 语法: SELECT column_name,column_name FROM table_name [WHERE Clause…

quartz mysql索引_分布式系统中的定时任务全解(二)

在实际项目中&#xff0c;通常需要用到定时任务(定时作业)&#xff0c;spring框架提供了很好的实现。 1、 下载spring-quartz插件包 这里默认当前系统中是集成了spring框架的基本功能的。去网上下载spring定时器的jar包&#xff0c;这里用的是quartz-all-1.8.4.jar&#xff0c…

react div组件设置可点击不可点击_React面试全解

更新:收藏前点个赞亲&#xff0c;为啥我每次写的东西收藏都是赞的n倍&#xff01;&#xff01;花了一个月时间总结的React面试题 希望能帮助到你全文近万字建议保存仔细过一遍目录面试中常提的重要概念React生命周期ReduxRouter重要的方法面试中常提的重要概念1 什么是模块化是…

【牛客 - 185B】路径数量(离散数学,长度为k的路径数量,图)

题干&#xff1a; 给出一个 n * n 的邻接矩阵A. A是一个01矩阵 . A[i][j]1表示i号点和j号点之间有长度为1的边直接相连. 求出从 1 号点 到 n 号点长度为k的路径的数目. 输入描述: 第1行两个数n,k (20 ≤n ≤ 30,1 ≤ k ≤ 10) 第2行至第n1行&#xff0c;为一个邻接矩阵 …

【牛客 - 368D】动态连通块(并查集+bitset优化)

题干&#xff1a; 小T有n个点&#xff0c;每个点可能是黑色的&#xff0c;可能是白色的。 小T对这张图的定义了白连通块和黑连通块&#xff1a; 白连通块&#xff1a;图中一个点集V&#xff0c;若满足所有点都是白点&#xff0c;并且V中任意两点都可以只经过V中的点互相到达&a…