zoukankan      html  css  js  c++  java
  • Match:Period(POJ 1961)

                 

                 Period

      题目大意:给定一个字符串,要你找到前缀重复了多少次

      思路,就是kmp的next数组的简单应用,不要修正next的距离就好了,直接就可以跳转了

      PS:喝了点酒用递归实现除法和取余了。。。结果tle不知道怎么回事。。。

      

     1 #include <iostream>
     2 #include <functional>
     3 #include <algorithm>
     4 #include <string>
     5 
     6 using namespace std;
     7 
     8 typedef int Positon;
     9 void Get_Next(const int);
    10 
    11 static int _next[1000002];
    12 static char str[1000002];
    13 
    14 int main(void)
    15 {
    16     int str_length, k_count, if_res;
    17     int case_count = 1;
    18     
    19     while (~scanf("%d", &str_length))
    20     {
    21         if (str_length == 0)break;    
    22         getchar();
    23 
    24         scanf("%s", str);
    25 
    26         Get_Next(str_length);
    27         printf("Test case #%d
    ", case_count++);
    28         for (int i = 2; i <= str_length; i++)
    29         {
    30             k_count = i / (i - _next[i]);
    31             if_res = i % (i - _next[i]);
    32             if (if_res == 0 && k_count > 1)
    33                 printf("%d %d
    ", i, k_count);
    34         }
    35         cout << endl;
    36     }
    37     return EXIT_SUCCESS;
    38 }
    39 
    40 void Get_Next(const int str_length)
    41 {
    42     Positon i = 0, k = -1;
    43     _next[0] = -1;
    44 
    45     while (i < str_length)
    46     {
    47         if (k == -1 || str[i] == str[k])
    48         {
    49             i++;
    50             k++;
    51             _next[i] = k;
    52         }
    53         else k = _next[k];
    54     }
    55 }

      

      这题用STL的string会卡很长的时间,很奇怪,加了std::ios::sync_with_stdio(false);都没用

  • 相关阅读:
    [原]音视频播放笔记
    [原]很多时候, 错误源于自己
    [原]找工作之tj
    [原]昨天碰到的一个诡异问题
    [原]硬盘分区规划
    [原]编程手记2008.08.26
    [原]编程手记2008.08.28
    eclipse 某些java文件乱码
    图片垂直居中,兼容ie6
    ul里不能直接嵌套div
  • 原文地址:https://www.cnblogs.com/Philip-Tell-Truth/p/5180670.html
Copyright © 2011-2022 走看看