zoukankan      html  css  js  c++  java
  • chaper3_exerise_UVa455_周期串

     1 #include<iostream>
     2 #include<cstring>
     3 #include<stdio.h>
     4 using namespace std;
     5 const int maxn = 1010;
     6 
     7 int equ(const char *a,int beg,int beh)        //总是与首字符形成的串相比,beh为后一个的序列 
     8 {
     9     int n = strlen(a), i;
    10     for (i = 0; i < n; i++)
    11     {
    12         if (a[i] != a[(beh+i)%n])
    13             return 0;
    14     }
    15     return beh;
    16 
    17 }
    18 int main(void)
    19 {
    20     int T;
    21     char a[maxn];
    22     cin >> T;
    23     while (T--)
    24     {
    25         int count = 0;
    26         cin >> a;
    27         int ans = 0;
    28         for (int i = 1; i < strlen(a); i++)
    29         {
    30             if (equ(a,0,i)) {       //后面开始形成的串如果和首字符形成的(环形)串相同,即后面的(位置)为周期 
    31                 count = equ(a,0,i);
    32                 break;
    33             }
    34         }
    35         if (count)
    36             cout << count << endl;
    37         else
    38             cout << strlen(a) << endl; //如果没有,即输入的字符长度为周期 
    39         if (T > 0)                     //不要多输出换行 
    40             cout << endl;
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    C++结构体中sizeof
    sizeof()的用法
    XStream和Json
    省市联动
    ajax
    配置文件的读取
    JSP标签库
    字符串函数参数传入传出(去空格)
    字符串函数参数传入传出(字符串反转)
    opendir,readdir,closedir
  • 原文地址:https://www.cnblogs.com/douzujun/p/5584840.html
Copyright © 2011-2022 走看看