• Match:Power Strings(POJ 2406)


                    

                    字符串前缀的阶

      题目大意:求前缀的阶

      和POJ1961是一样的,KMP的Next数组的应用,不要用STL,不要一个一个读入字符(IO永远是最慢的)

      

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <functional>
     4 #include <string.h>
     5 
     6 using namespace std;
     7 
     8 static char Text[1000002];
     9 static int _Next[1000002];
    10 
    11 void Input(int &);
    12 void Get_Next(const int);
    13 
    14 int main(void)
    15 {
    16     int Length, k_count, if_res;
    17     while (1)
    18     {
    19         Input(Length);
    20         if (*Text == '.')//小数点结束
    21             break;
    22         Get_Next(Length);
    23         if_res = Length % (Length - _Next[Length]);
    24         k_count = Length / (Length - _Next[Length]);
    25         if (if_res == 0 && k_count > 1)
    26             printf("%d
    ", k_count);
    27         else printf("1
    ");
    28     }
    29     return EXIT_SUCCESS;
    30 }
    31 
    32 void Input(int &Length)
    33 {
    34     Length = 0;
    35     scanf("%s", Text);
    36     Length = strlen(Text);
    37 }
    38 
    39 void Get_Next(const int Length)
    40 {
    41     int i = 0, k = -1;
    42     _Next[0] = -1;
    43 
    44     while (i < Length)
    45     {
    46         if (k == -1 || Text[i] == Text[k])
    47         {
    48             i++;
    49             k++;
    50             _Next[i] = k;
    51         }
    52         else k = _Next[k];
    53     }
    54 }

      

      另外以这一题有散列做法,但是没有kmp那么快,但是用到了一个非常好的算法——矩阵快速幂,以后补

  • 相关阅读:
    第二次作业——评分!
    第一次点评!
    神经网络测试:利用分块patch输入的弊端
    利用分块进行网络输入测试
    python 用filter求解素数
    英语语法
    git clone 下载出现Time out
    路由转发
    获取用户密码
    后门维持
  • 原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/5181610.html
Copyright © 2020-2023  润新知