zoukankan      html  css  js  c++  java
  • uestc 1703一道更简单的字符串题目

    https://vjudge.net/problem/UESTC-1703

    题意:略

    思路:

    枚举+字符串hash。

    ans从1到len开始枚举字符串的长度,然后就依次比较各段长度为ans的字符串的hash值是否和hash(0,ans)的hash值相等。对于剩余的长度为tlen小于长度为ans的字符串,比较它的hash和hash(0,tlen)。一旦遇到一个满足条件的ans,直接跳出,就可以保证这个是最小的了。

    代码:

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 char s[1000005];
     5 
     6 unsigned int mha(int st,int len)
     7 {
     8     unsigned int sed = 131;
     9     unsigned int h = 0;
    10 
    11     for (int i = 0;i < len;i++)
    12     {
    13         h = h * sed + s[st+i];
    14     }
    15 
    16     return (h & 0x7FFFFFFF);
    17 }
    18 
    19 int main()
    20 {
    21     scanf("%s",s);
    22 
    23     int len = strlen(s);
    24 
    25     int ans;
    26 
    27     for (int i = 1;i <= len;i++)
    28     {
    29         bool f = 0;
    30 
    31         int code = mha(0,i);
    32 
    33         for (int j = 0;j + i < len;j += i)
    34         {
    35             int tmp = mha(j,i);
    36 
    37             if (tmp != code)
    38             {
    39                 f = 1;
    40                 break;
    41             }
    42         }
    43 
    44         int re = len % i;
    45 
    46         code = mha(0,re);
    47 
    48         int tmp = mha(len-re,re);
    49 
    50         if (tmp != code) f = 1;
    51 
    52         if (!f)
    53         {
    54             ans = i;
    55             break;
    56         }
    57     }
    58 
    59     printf("%d
    ",ans);
    60 
    61     return 0;
    62 }
  • 相关阅读:
    mysql数据类型
    linux执行shell脚本
    mysql常用命令
    CentOS 安装man man-pages
    inet_pton inet_ntop inet_aton htonl
    Makefile
    python---if、while、for
    python---变量、常量、注释、基本数据类型
    python---概述
    数组和广义表(列表)
  • 原文地址:https://www.cnblogs.com/kickit/p/7237968.html
Copyright © 2011-2022 走看看