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 }
  • 相关阅读:
    生鲜购物篮模型
    shell脚本
    一号店评论文本聚类研究1
    数据框的合并(根据某一个字段)
    Reshape包
    R-kmeans
    python练习(续)
    python练习
    截取整数字符串
    java中PriorityQueue优先队列使用方法
  • 原文地址:https://www.cnblogs.com/douzujun/p/5584840.html
Copyright © 2011-2022 走看看