zoukankan      html  css  js  c++  java
  • HDU 3336 Count the string

     1 //this problem is same like to (POJ)Seek the Name, Seek the Fame
     2 #include <iostream>
     3 #include <cstring>
     4 #include <string>
     5 #include <algorithm>
     6 #define N 200010
     7 using namespace std;
     8 
     9 char a[N];
    10 int Next[N];
    11 int Len;
    12 int dp[N];//dp[i] represents from 1 ~ i strings, how many prefixs are end by i
    13 const int mod = 10007;
    14 
    15 void GetNext()
    16 {
    17     int i = 0, j = Next[0] = -1;
    18     while (i < Len)
    19     {
    20         if (j == -1 || a[i] == a[j])
    21             Next[++i] = ++j;
    22         else
    23             j = Next[j];
    24     }
    25 }
    26 
    27 int main(void)
    28 {
    29     ios::sync_with_stdio(false);
    30     int cas;
    31     cin >> cas;
    32     while (cas--)
    33     {
    34         cin >> Len >> a;
    35         GetNext();
    36         int res = 0;
    37         memset(dp, 0, sizeof(dp));
    38         for (int i = 1; i <= Len; ++i)
    39         {
    40             dp[i] = (dp[Next[i]] + 1) % mod;// eg:(1 + 1) % mod <=> 1 % mod + 1 % mod
    41             res = (res + dp[i]) % mod;
    42         }
    43         cout << res << endl;
    44     }
    45 
    46     return 0;
    47 }
  • 相关阅读:
    linux常用命令
    练习00004
    python学习第六天
    练习00003
    练习00002
    python学习第四天
    练习00001
    Linux_安装mysql踩坑日记
    Linux_更改远程登录端口以及禁止root用户登录
    redis_基础_基本使用
  • 原文地址:https://www.cnblogs.com/ducklu/p/8989223.html
Copyright © 2011-2022 走看看