TOJ4127: Root of String
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4127
4127: Root of String
时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte
描述
The root of string S is a shortest string T such that S=TTTTT...T. That is, you can obtain S by concatenating T with itself many times.
输入
Each case has a line with one non-empty string (no more than 255 characters) over the alphabet a,b,c,...,z.
输出
For each case, output the root of the string.
样例输入
aaaaaa
ababab
abca
样例输出
a
ab
abca
思路:题意是找字符串的循环节,考虑到循环节的长度只可能是本身长度的因数,即可以从1开始,遍历因数长度的所有情况,如果能找到循环节,那么就输出,如果找不到,那么循环节就是自己本身。
分割字符串可以调用string类中的substr函数。
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
int main(){
string s;
while(cin>>s){
int a[],ans = ;
for(int i = ; i < s.size() ; i++){
if(s.size() % i == ){
a[ans++] = i;
}
}//循环节肯定是字符串长度的因数
int f = ;//记录是否找到比本身短的字符串作为 循环节
for(int i = ; i < ans; i++){//从小到大遍历因数 长度的循环节
int flag = ;//记录是否找到循环节
int k = s.size()/a[i]; //记录个数
string str[];//保存分割的字符串
for(int j = ; j < k ; j++){
int x = j * a[i];
str[j] = s.substr(x,a[i]);
}
//从x位置开始,每 a[i]个长度分割字符串,调用substr函数分割字符串
for(int j = ; j < k ; j++){
if(str[j] != str[j-]){
flag = ;break;
}
}
if(flag){
f = ;
cout<<str[]<<endl;
break;
}//遍历分割好的字符串,如果是循环节就输出
}
if(f)cout<<s<<endl;//如果找不到比本身小的循环节,那么自身就是循环节
}
}
TOJ4127: Root of String的更多相关文章
- [Swift]LeetCode129. 求根到叶子节点数字之和 | Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
- [Swift]LeetCode988. 从叶结点开始的最小字符串 | Smallest String Starting From Leaf
Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to ...
- 实体转xml 并以string输出
<?xml version="1.0" encoding="utf-8"?><Root> <Header> <Mess ...
- 转:Ogre源码分析之Root类、Facade模式
Ogre源码分析(一)Root类,Facade模式 Ogre中的Root对象是一个Ogre应用程序的主入口点.因为它是整个Ogre引擎的外观(Façade)类.通过Root对象来开启和停止Ogre是最 ...
- LC 988. Smallest String Starting From Leaf
Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to ...
- Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- [原创]java使用JDBC向MySQL数据库批次插入10W条数据测试效率
使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(100000),如何提高效率呢?在JDBC编程接口中Statement 有两个方法特别值得注意:通过使用addBatch( ...
- Java开发中的23种设计模式详解
[放弃了原文访问者模式的Demo,自己写了一个新使用场景的Demo,加上了自己的理解] [源码地址:https://github.com/leon66666/DesignPattern] 一.设计模式 ...
随机推荐
- centos7.5安装VirtualBox
centos7.5安装minikube时要求先安装VirtualBox 1.准备repo文件 [root@localhost yum.repos.d]# pwd /etc/yum.repos.d [r ...
- docker tomcat镜像制作
推荐使用dockerfile(本文直接拉取tomcat需要进入容器自行安装vim):docker利用Dockerfile来制作镜像 1.查找Docker Hub上的tomcat镜像 [root@loc ...
- nginx的websock问题解决
生产环境中有一个项目需要使用到websock,但是项目上线后发现websock连接后马上断开,但是在测试环境没有问题,后来就想到配置文件和nginx版本问题 核对后发现,nginx和配置文件都是相同的 ...
- python 任务调度模块sched
类似于crontab的功能,可以实现定时定点执行任务; 将已经生成的任务放入队列中,获取到了执行可以实现任务调度功能; 如果将需求复杂化,加上优先级策略,并能取消已经加入队列中的任务,就需要使用pyt ...
- ArcGIS模型构建器案例教程-批量复制工作空间所有要素类
ArcGIS模型构建器案例教程-批量复制工作空间所有要素类 目的:批量复制工作空间所有要素类 工具名称:WorkspaceCopyFeatureClasses 使用方法:输入工作空间,指定输出工作空间 ...
- 创建DLL动态链接库——声明导出法
DLL声明导出法:是通过使用__declspec(dllexport),添加到需要导出的函数前,进行声明. 头文件定义如下(OPdll.h): 源文件定义如下(OPdll.cpp): 通过以上两个文件 ...
- Java学习03 (第一遍)
Java是面向对象的语言,函数是面向过程语言的叫法,比如C语言,在Java中一般称之为方法. 构造方法的作用是实例化对象,每个类中都有,即使不写程序也会分配一个默认无参数的构造方法. Java中都是对 ...
- 微信小程序 在canvas画布上划动,页面禁止滑动
要实现微信小程序 在canvas画布上划动,页面禁止滑动,不仅要设置disable-scroll="true",还要要给canvas绑定一个触摸事件才能生效. <canvas ...
- 搭建GitLab+Jenkins
1. Jenkins and GitLab Jenkins是一个自动化服务器,可以运行各种自动化构建.测试或部署任务. GitLab是一个代码仓库,用来管理代码. 两者结合起来,就可以实现开发者提交代 ...
- js正则表达式子校验
//正则表达式校验new RegExp(/^[1-9]\d{4,8}$/,"g").test(1234);//执行一个字符串所表达的方法 eval(this['字符串']) 正则表 ...