写炸,上网,不同KMP形态。

无力,照该,一换写法就过。

横批:我是垃圾

求\(next\)时\(DP\)出\(num\),路径压缩防卡\(n^2\)

AC

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long #define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N = 1000007;
const int mod = 1000000007; int nxt[N], num[N];
char str[N];
int main() {
//FileOpen(); int n;
io >> n;
while(n--) {
scanf("%s", str);
int len = strlen(str);
int ans = 1;
R(i,1,len) nxt[i] = 0;
nxt[0] = -1;
num[0] = 0, num[1] = 1;
int i = 0, j = -1;
while(i < len){
if(j == -1 || str[i] == str[j]){
nxt[++i] = ++j;
num[i] = num[j] + 1;
}
else
j = nxt[j];
}
i = 0, j = -1;
while(i < len){
if(j == -1 || str[i] == str[j]){
++i, ++j;
while((j << 1) > i + 1) j = nxt[j];
ans = ans * 1ll * (num[j] + 1) % mod;
}
else
j = nxt[j];
} printf("%d\n", ans);
}
return 0;
}

全\(WA\)的原始代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long #define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N = 1000007;
const int mod = 1000000007; int nxt[N], num[N];
char str[N];
int main() {
//FileOpen(); int n;
io >> n;
while(n--) {
scanf("%s", str);
int len = strlen(str);
int ans = 1;
R(i,1,len) nxt[i] = 0;
nxt[0] = -1;
num[0] = 0, num[1] = 1;
int i = 0, j = -1;
while(i < len){
if(j == -1 || str[i] == str[j]){
nxt[++i] = ++j;
num[i] = num[j] + 1;
}
else
j = nxt[j];
}
i = 0, j = -1;
while(i < len){
if(j == -1 || str[i] == str[j]){
++i, ++j;
while((j << 1) > i + 1) j = nxt[j];
ans = ans * 1ll * (num[j] + 1) % mod;
}
else
j = nxt[j];
} printf("%d\n", ans);
}
return 0;
}

最新文章

  1. php+mysql
  2. 【转】解决eclipse无法设置NDK问题
  3. POJ 2838 单调队列
  4. 日常css和js小知识点记录
  5. ZOJ 3644 Kitty&#39;s Game dfs,记忆化搜索,map映射 难度:2
  6. struts2上传下载
  7. PAT (Advanced Level) 1066. Root of AVL Tree (25)
  8. js原生拓展网址——mozilla开发者
  9. EMC题
  10. 说一说MVC的CompressActionFilterAttrubute(五)
  11. Linux基础命令---uname显示计算机名称
  12. Android--小游戏
  13. 初学Python——文件操作第二篇
  14. 吉特日化MES-日化生产称料基本步骤
  15. windows10 vs2017 C++连接MySQL
  16. luogu 4401 矿工配餐 多维dp
  17. Hadoop – The Definitive Guide Examples,,IntelliJ
  18. 课程一(Neural Networks and Deep Learning),第四周(Deep Neural Networks)—— 1.Practice Questions: Key concepts on Deep Neural Networks
  19. poj3208 Apocalypse Someday 数位dp+二分 求第K(K &lt;= 5*107)个有连续3个6的数。
  20. HDU 6153 A Secret(扩展kmp)

热门文章

  1. .net 获取IP地址的几种方式
  2. uni-simple-router
  3. ACW:831. KMP字符串
  4. java基础题(4)
  5. Spring IOC 常用注解与使用
  6. torch.nn.MSELoss()函数解读
  7. 解决Docker运行命令时提示&quot;Got permission denied while trying to connect to the Docker daemon socket&quot;类情况
  8. c++ 辗转相除(动图)
  9. .NET中检测文件是否被其他进程占用
  10. 2分钟实现一个Vue实时直播系统