博客
关于我
蓝桥杯 周期子串 C++算法提高 HERODING的蓝桥杯之路
阅读量:155 次
发布时间:2019-02-28

本文共 1039 字,大约阅读时间需要 3 分钟。

资源限制

时间限制:1.0s 内存限制:256.0MB
问题描述
  右右喜欢听故事,但是右右的妈妈总是讲一些“从前有座山,山里有座庙,庙里有个老和尚给小和尚讲故事,讲的什么呢?从前有座山……”这样循环的故事来搪塞右右。
  我们定义,如果一个字符串是以一个或者一个以上的长度为k的重复字符串所连接成的,那么这个字符串就叫做周期为k的串。
  例如:
  字符串’abcabcabcabc’周期为3,因为它是由4个循环’abc’组成的。它同样是以6为周期(两个重复的’abcabc’)和以12为周期(一个循环’abcabcabcabc’)。
  右右现在想给他的朋友大灰狼转述妈妈讲的故事,请帮他写一个程序,可以测定一个字符串的最小周期。
输入格式
  一个最大长度为100的无空格的字符串。
输出格式
  一个整数,表示输入的字符串的最小周期。
样例输入
HaHaHa
样例输出
2
样例输入
Return0
样例输出
7

解题思路:

这题可以使用暴力解法,就是找到串长度的余数为0的子串,不断遍历,判断是否循环,然后得出结果,思路很清晰,代码如下 :

#include
using namespace std; int main(){ char s[100];//输入的字符串 int res; scanf("%s", s); int len = strlen(s);//输入的字符串长度 bool flag = false; for(int i = 1; i <= len; i ++){ char temp[100]; if(len % i == 0){ for(int j = 0; j < i; j ++){ temp[j] = s[j];//赋初值 } int index = 0; for(int l = i; l <= len; l ++){ if(l == len){ flag = true; break; } if(temp[index] == s[l]){ index = (index + 1) % i; continue; }else{ break; } } } if(flag){ res = i; break; } } cout << res << endl; return 0;}

转载地址:http://ockj.baihongyu.com/

你可能感兴趣的文章
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>