传送门: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的更多相关文章

  1. [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 ...

  2. [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  ...

  3. 实体转xml 并以string输出

    <?xml version="1.0" encoding="utf-8"?><Root> <Header> <Mess ...

  4. 转:Ogre源码分析之Root类、Facade模式

    Ogre源码分析(一)Root类,Facade模式 Ogre中的Root对象是一个Ogre应用程序的主入口点.因为它是整个Ogre引擎的外观(Façade)类.通过Root对象来开启和停止Ogre是最 ...

  5. 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  ...

  6. Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  7. 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  8. [原创]java使用JDBC向MySQL数据库批次插入10W条数据测试效率

    使用JDBC连接MySQL数据库进行数据插入的时候,特别是大批量数据连续插入(100000),如何提高效率呢?在JDBC编程接口中Statement 有两个方法特别值得注意:通过使用addBatch( ...

  9. Java开发中的23种设计模式详解

    [放弃了原文访问者模式的Demo,自己写了一个新使用场景的Demo,加上了自己的理解] [源码地址:https://github.com/leon66666/DesignPattern] 一.设计模式 ...

随机推荐

  1. 文件系统(File System)

    什么是文件系统,引用百科解释: 操作系统中负责管理和存储文件信息的软件机构称为文件管理系统,简称文件系统. 文件系统是操作系统核心的组成部分,没有它我们无法完成对文件的增.删.改.查等基本操作 概念 ...

  2. android stuido的代码排版的快捷建CTRL+ALT+L

    CTRL+ALT+L 需要主要留意的地方是QQ的与其冲突的 我将qq的中快捷方式给去除

  3. SVM视频跟踪

    # -*- coding: utf-8 -*- """ Created on Thu Nov 8 21:44:12 2018 @author: xg "&quo ...

  4. eclipse中导入java类失败的问题

    在 Eclips 开发时,新建了一个 Dynamic Web Project,在运行jsp文件时tomcat报错: org.apache.jasper.JasperException: Unable ...

  5. Zookeeper 3、Zookeeper工作原理(转)

    1.Zookeeper的角色 » 领导者(leader),负责进行投票的发起和决议,更新系统状态 » 学习者(learner),包括跟随者(follower)和观察者(observer),follow ...

  6. JS基础一-入门知识

    一.什么是JavaScript JavaScript是一种基于对象和事件驱动并具有安全性能的解释性脚本语言.JavaScript不需要编译,直接嵌入在HTTP页面中,把静态页面转变成支持用户交互并响应 ...

  7. 跨域(一)——CORS机制

    Ajax是严格遵守同源策略的,既不能从另一个域读取数据,也不能发送数据到另一个域.但是,W3C的新标准中CORS(Cross Origin Resource Sharing)推进浏览器支持这样的跨域方 ...

  8. Zabbix邮件报警配置

    一.安装sendmail或者postfix yum install sendmail #安装 service sendmail start #启动 chkconfig sendmail on #设置开 ...

  9. 为什么虚拟DOM更优胜一筹

    注意: 虚拟DOM只是实现MVVM的一种方案,或者说是视图更新的一种策略.没有虚拟DOM比MVVM更好一说. 我们回顾传统MVC框架,如backbone,它是将某个模板编译成模板函数,需要更新时,是自 ...

  10. C# 读取ini文件,读不出来原因

    先赋上相关读取ini文件代码 public class INIHelper { public string inipath; [DllImport("kernel32")] pri ...