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

    #include <stdio.h>
    #include <string.h>
    int main()
    {
     char word[100];int ok=0;
     scanf("%s",word);
     int l=strlen(word);
     for (int i=1; i<=l; ++i)//周期必是1到l,若为1,即是常数串
     {
      if (l%i==0)//最小周期必是长度的约数
      {
       int ok=1;
       for (int j=i; j<l; ++j)
       {
        if (word[j%i]!=word[j])//个人认为j-
    //word[j]为第二周期的第一个字符,word[j%i]是前一个周期的第一个字符(因为
    //j=i;j%i=0;j每加1,j%i也加1,直到最后一组word[j]都等于word[j%i],则i为周期
         {
          ok=0;
          break;
         }
       }
       if (ok)
       {
        printf("%d\n",i);
        break;// this is the least cycle
       }
      }
      else
       continue;
     }
     return 0;
    }

    上述代码,无MMP时输出了长度

    #include <stdio.h>
    #include<stdlib.h>
    #include <string.h>
    int main()
    {
    char word[100];
    scanf("%s",word);
    int l=strlen(word);
    for (int i=1; i<=l; ++i)//周期必是1到l,若为1,即是常数串
    //加上等号时,最小正周期为长度
    {
    if (l%i==0)//最小周期必是长度的约数
    {
    int ok=1;
    for (int j=i; j<l; ++j)
    {
    if (word[j%i]!=word[j])//个人认为j-1亦可
    //word[j]为第二周期的第一个字符,word[j%i]是前一个周期的第一个字符(因为
    //j=i;j%i=0;j每加1,j%i也加1,直到最后一组word[j]都等于word[j%i],则i为周期
    {
    ok=0;
    break;
    }
    }
    if (ok&&i!=l)
    {
    printf("%d\n",i);
    break;// this is the least cycle
    }
    else if(i==l)
    printf("No MMP\n");
    }
    }
    system("pause");
    return 0;
    }

  • 相关阅读:
    c#个人记录常用方法(更新中)
    Newtonsoft.Json.dll解析json的dll文件使用
    组织http请求
    ado.net中的几个对象
    jquery-easyui使用
    aspx与mvc页面验证码
    aspx页面状态管理(查询字符串Request与Application)
    aspx页面状态管理Cookie和ViewState
    在网页中插入qq连接
    ASP.NET中上传图片检测其是否为真实的图片 防范病毒上传至服务器
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2446205.html
Copyright © 2011-2022 走看看