HDU3746 Cyclic Nacklace 【KMP】

【字符串入门专题1】 E - Cyclic Nacklace hdu3764 【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 阅读详情

Cyclic Nacklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2538    Accepted Submission(s): 1154


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

题意:给定一个串,求至少添加几个字符能使其内部至少重复两次的重复串。

题解:直接对所给串求next数组,len-next[len]即为循环节点。

#include <stdio.h>
#define maxn 100002

char str[maxn];
int len, next[maxn];

void getNext()
{
	int i = 0, j = -1;
	next[0] = -1;
	while(str[i]){
		if(j == -1 || str[i] == str[j]){
			++i; ++j; next[i] = j;
		}else j = next[j];
	}
	len = i; //save time
}

int main()
{
	int cas, ans;
	scanf("%d", &cas);
	while(cas--){
		scanf("%s", str);
		getNext();
		ans = len - next[len];
		if(ans != len && len % ans == 0)
			ans = 0;
		else ans = ans - next[len] % ans;
		printf("%d\n", ans);
	}
	return 0;
}


[hdu 3746] Cyclic Nacklace [kmp] 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 一句话题意: 给一个字符串S,问最少再在串尾加多少字符,才能使得S成为一个有循环节的字符串。判len-next[len] 能否整除 len 。 若整除 ans=0 若不整除ans=(len-next[len])-len%(len-next[len])next()函数的优化版本 vo 阅读详情

相关推荐

手把手教你用VM实现欧姆龙PLC数据采集(含Fins协议配置详解)

本文详细介绍了如何使用VM(VisionMaster)实现与欧姆龙PLC的数据采集,重点解析了Fins协议(包括Fins TCP/UDP)的配置步骤。从网络环境搭建、PLC的IP地址设置,到VM端Fins通讯通道的详细配置与数据点映射,提供了完整的实战指南。文章还涵盖了数据收发解析、常见问题排查清单,帮助工程师快速完成视觉系统与PLC的稳定集成。

weixin_29171087的博客 192

hdu3746 Cyclic Nacklace

Cyclic Nacklace Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 1   Accepted Submission(s) : 1 Font: Times New Roman | Verdana | Georgia Font

夏天的风 2409

【FOC无刷电机控制】六步换向、FOC,STM32cubemx从零开始搭建BLDC六步换相代码、FOC代码(基于霍尔传感器)

FOC、六步换相,无刷电机控制

xifan的博客 3万+

字符串——Seek the Name, Seek the Fame(kmp应用)

思路:一般的kmp处理完每个位置都只会有一个next值,所以这里并不能。但kmp具有一个很强的性质。题意:给一个字符串s,要求所有最后一个字符结尾的相同前缀和后缀的长度,自身也算。

测试 568

从“Segmentation fault”到流畅运行:深入解析CARLA Engine crash与Signal 11报错排查指南

本文深入解析了CARLA仿真平台运行时遭遇的Engine crash与Signal 11(Segmentation fault)报错。文章指出,该错误的根本原因常与NVIDIA显卡驱动版本不兼容有关,并提供了从日志分析、常见方案试错到驱动安全降级的完整排查指南,帮助用户从崩溃走向流畅运行。

pca5navigator的博客 469

Lumerical FDTD新手必看:从零开始搭建光子晶体仿真环境(附完整脚本)

本文为Lumerical FDTD新手提供光子晶体仿真环境的完整搭建指南,涵盖从基础参数设置到脚本编写的全流程。通过典型光子晶体带隙分析案例,详细讲解结构建模、仿真参数配置及后处理技巧,并附赠完整MATLAB脚本,帮助用户快速掌握FDTD仿真核心技术。

3c4x5z6v7b的博客 1058

Cyclic Nacklace(循环项链)(kmp经典模板题) HDU - 3746

题目:Cyclic Nacklace(循环项链)中文大意: 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.

R_711的博客 590

【计算机科学导论实验】加法图灵机模拟 题解

本文提供计算机科学导论课程“图灵机加法器”实验的个人思路,仅供参考。

Chavapa's blog 2588

Cyclic Nacklace(kmp算法)

Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 799Accepted Submission(s): 343 Problem Description CC...

weixin_34346099的博客 525

[医学图像分割综述] Medical Image Segmentation Using Deep Learning: A Survey

本综述有两个独创之处,首先,与传统的调查方法直接将医学图像分割的文献划分为多个组,并对每个组详细介绍文献的方法相比,我们将目前流行的文献按照从粗到细的多层次结构进行分类。其次,本文重点研究了监督学习和弱监督学习方法,但未包括无监督方法,这种方法目前在医学图像领域并不流行。对于监督学习方法,我们从骨干网络的选择、网络块的设计和损失函数的改进三个方面对文献进行了分析。对于弱监督学习方法,我们分别根据数据增强,迁移学习和交互式分割研究文献。与已有的调查相比,本次调查的文献分类与以往有很大的不同,更便于读者了解相关

qq_38932073的博客 4万+

电容放电测试解析

电子设备断电后滤波电容残留电荷存在触电风险,需符合IEC等安全标准对放电时间的要求。放电测试需测量电容电压下降至安全值的时间,设计可采用主动放电电路或被动放电电阻。常见问题包括电阻选择不当或电路设计缺陷,建议预留放电时间裕度并采用组合放电方案。合规的放电设计是确保产品安全性和可靠性的关键环节。

硬核科技 814

【KMP】 hdu3746 Cyclic Nacklace

Cyclic Nacklace http://acm.hdu.edu.cn/showproblem.php?pid=3746 Problem Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, wit

Ted's 游乐园 852

【一看就会】Autoware.universe中静态绕障路线的生成逻辑

在路径行为规划中,前期根据地图和起点终点,生成了一组连续可行的路径段序列(lanelets),然后后续路径行为规划,又会进行起始点,终止点,避障绕障路径的路径规划,并且会调用route_handler中的函数,进行中心线生成,最后生成路径,后续再给速度行为规划进行速度规划。在autoware.universe的规划中,是基于规则的规划,而其中的规则又是为了解决不同场景的。而这些场景,大部分是为了解决路上障碍物(静态、动态、行人、车辆等等)。

weixin_48386130的博客 324

hdu 3746 Cyclic Nacklace(KMP)

题目链接:hdu 3746 Cyclic Nacklace 题目大意:问说最少需要在添加几个值,可以是字符串变成以一个子字符串循环得到的(至少有两个该子串) 解题思路:KMP的变形吧,将字符串的next数组求出来,next[n]是关键;k = n - next[n],如果k = 0的话,说明没有一个匹配的字符,那么至少就要添加n个;n%k = 0的话,说明该字符串已经满足了要求

不慌不忙、不急不躁 1604

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

KMP+hash hdu3746 Cyclic Nacklace

传送门:点击打开链接 题意:已知原串,在首或者尾加上一些字符后,变成至少有一个循环节循环2次变成新串 思路:我们能很容易的发现,在首添加字母和在尾添加字母效果是一样的,所以我们只考虑在末尾添加 首先我们都知道,KMP可以用来求前缀中的最长循环节长度。知道了这个有什么用呢? 我们枚举所有的前缀,然后取得循环节的长度w,与此时后面剩下的字符串的长度t去比较 如果后面剩下的字符串等于原字符串的

qwb的博客 1220

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

hdu 3746 Cyclic Nacklace kmp算法

这个问题考的是kmp算法中最小循环结点的问题 原问题去OJ看吧 http://acm.hdu.edu.cn/showproblem.php?pid=3746 #include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstr...

NoahCool 233

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

HDU 3746 Cyclic Nacklace KMP

题意:给出一个字符串,问还需要在后面添加多少个字符才能使它变成由一个前缀循环至少两遍而成。。 这边看到了一个写的很好的题解,还详细分析了next数组的作用,膜拜下。 代码: /* * Author: illuz * Blog: http://blog.csdn.net/hcbbt * File: hdu3746.cpp * Create Dat

水果君の日常 1649

HDU 3746 Cyclic Nacklace (KMP)

Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17218    Accepted Submission(s): 7101   Problem Description CC always become...

LSD20164388的博客 235

HDU 3746 Cyclic Nacklace【KMP】

Carneginon 传送门。 题意: T组数据 给一个串串,求最少加入多少珠子(只能在串串首尾加),将这个串串首尾相接后,是一个有循环的串串。 思路: next[]next[ ]next[] 数组的应用。 板子。 是时候开始认真学一学串串,锻炼锻炼代码能力了。 #include<cstdio> #include<cstring> const int M...

zhmeng 295

HDU 3746 - Cyclic Nacklace(KMP)

题目链接:   http://acm.hdu.edu.cn/showproblem.php?pid=3746 题目描述:   给定一个字符串S,问至少需要添加多少个字符,使得新字符串拥有一个以上的循环节。 解题思路:   又一个next数组的应用(由于next数组在C++内不能使用,所以我使用失配数组也即fail数组,其实两个数组都是一样的)。计算得出next数组后,由于题目要...

ayg046002的博客 145

HDU 3746 Cyclic Nacklace(KMP)

题意:给出一个字符串,问最少加几个字符可以时新的字符串拥有两个以上循环节 思路:只有当nxt[len]!=0时最后一个字符才可以再形成一个新的循环节,此时循环节的长度时len-nxt[len],分类讨论下就可以。最奇怪的一点是,如果直接用cin输入string时会wa,最后只能换成字符串的形式才可以a,这点是真的不知道为什么,但string会出现问题这点需要特别注意 代码: #includ...

圣诞老头的博客 296

HDU - 3746 Cyclic Nacklace (KMP)

https://vjudge.net/problem/HDU-3746 HDU - 3746 Cyclic Nacklace (KMP)题目AC代码 题目 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

斯文~ 193

Cyclic Nacklace HDU - 3746 (前缀数组 kmp)

用前缀数组可以求得初始字符串的最小循环节。 最小循环节=(原字符串长度-末尾前缀数组值)

weixin_43925900的博客 218

【HDU 3746 Cyclic Nacklace 】 KMP

HDU3746 本题题意为添加最少的字符使原字符串变成周期至少为2的循环字符串 用到模板里所说的,长度为len的字符串的最小循环节为len-next[len],求出最小循环节,算出最后应该补充多少就结束了。 求最小循环节的证明法请看此链接KMP求字符串最小循环节 HDU3746代码 #include&lt;stdio.h&gt; #include&lt;iostream&gt; #inc...

lajiyuan的博客 166
上一篇: HDU2594 Simpsons’ Hidden Talents 【KMP】
下一篇: POJ2406 Power Strings 【KMP】
长风Qiu
博客等级 码龄13年 165粉丝 709原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值