I. Where Am I?

这是一道关于字符串处理的问题,要求找到一个最小的K值,使得在包含N个不同字母的字符串中,任意连续K个字符的子序列都是唯一的。例如,对于字符串'ABCDABC',最小的K值是4,因为长度为4的所有子序列都是唯一的。解决这个问题的方法包括使用KMP算法或直接通过枚举子串并判断是否重复。
摘要由CSDN通过智能技术生成

GDUT 2020寒假训练 排位赛一 I

原题链接

题目

题目截图
Farmer John has gone out for a walk down the road and thinks he may now be lost! Along the road there are N farms (1≤N≤100) in a row. Farms unfortunately do not have house numbers, making it hard for Farmer John to figure out his location along the road. However, each farm does have a colorful mailbox along the side of the road, so Farmer John hopes that if he looks at the colors of the mailboxes nearest to him, he can uniquely determine where he is.

Each mailbox color is specified by a letter in the range A…Z, so the sequence of N mailboxes down the road can be represented by a string of length N containing letters in the range A…Z. Some mailboxes may have the same colors as other mailboxes. Farmer John wants to know what is the smallest value of K such that if he looks at any sequence of K consecutive mailboxes, he can uniquely determine the location of that sequence along the road.

For example, suppose the sequence of mailboxes along the road is ‘ABCDABC’. Farmer John cannot set K=3, since if he sees ‘ABC’, there are two possible locations along the road where this consecutive set of colors might be. The smallest value of K that works is K=4, since if he looks at any consecutive set of 4 mailboxes, this sequence of colors uniquely determines his position along the road.

Input
The first line of input contains N, and the second line contains a string of N characters, each in the range A…Z.

Output
Print a line containing a single integer, specifying the smallest value of K that solves Farmer John’s problem.

题目大意

就是求着含有N个大学字母的字符串中,不重复的子序列的最小长度。
比如“ABCDABC”中 子序列长度为2的有"AB" “BC” “CD” “DA” “AB” “BC"虽然没有和DA一样的子序列,但是有和其他三个一样的子序列,所以最小长度不是2;长度为3的有"ABC” “BCD” “CDA” “DAB” “ABC”,在以上四纵序列中ABC有重复的所以长荡湖不是3;子序列长度为4的有“ABCD”“BCDA”“CDAB”“DABC”都不重复,所以最小长度为4

思路1

枚举
枚举区间长度,从2一直枚举到n 枚举区间长度为k的所有子序列,然后对子序列一一进行KMP,KMP能寻找原串中与目标串匹配的子序列,得出匹配部分的第一个字母在原串的位置。那么找到匹配后进行计数,计数超过2个则说明原串中有重复的子序列,那么就换k+1的区间。
(写解题报告的时候忽然觉得KMP麻烦了,直接枚举出k区间的所有子串,扔到set里面判重就好了,KMP属实麻烦了&#x

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值