首页 > 其他 > 详细

周期串

时间:2017-08-16 10:46:59      阅读:303      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <string.h>
using namespace std;

int main(){
    string str;
    cin >> str;
    for(int i = 1; i <= str.length(); i++){
        if(str.length() % i == 0){
            bool flag = true;
            for(int j = i; j < str.length(); j++){
                if(str[j] != str[j % i]){
                    flag = false;
                    break;
                }
            }
            if(flag == true){
                cout << i << endl;
                break;
            }
        }
    }
    return 0;
}

 

 1 import java.util.*;
 2 
 3 public class Cyclestring{
 4     public static void main(String[] args){
 5         String str;
 6         Scanner cin = new Scanner(System.in);
 7         str = cin.next();
 8         for(int i = 1; i <= str.length(); i++){
 9             if(str.length() % i == 0){
10                 boolean flag = true;
11                 for(int j = i; j < str.length(); j++){
12                     if(str.charAt(j) != str.charAt(j % i)){
13                         flag = false;
14                         break;
15                     }
16                     if(flag == true){
17                         System.out.println(i);
18                         break;
19                     }
20                 }
21             }
22         }
23     }
24 }

 

周期串

原文:http://www.cnblogs.com/jxust-jiege666/p/7371824.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!