写炸,上网,不同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;
}

Luogu2375 [NOI2014]动物园 (KMP)的更多相关文章

  1. BZOJ3670:[NOI2014]动物园(KMP)

    Description 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学向游客要吃的,园长决定开设算法班,让动物们学习 ...

  2. BZOJ 3670: [Noi2014]动物园 [KMP]

    求这玩意: 对于字符串S的前i个字符构成的子串,既是它的后缀同时又是它的前缀,并且该后缀与该前缀不重叠,将这种字符串的数量记作num[i] 对1,000,000,007取模的结果 n≤5,L≤1,00 ...

  3. [NOI2014]动物园(kmp)

    题目 https://www.luogu.org/problemnew/show/P2375 做法 查找多少个前缀与后缀配对,其实就是\(fail\)树的深度 而不可重叠,其实\(i\)不可用的,\( ...

  4. BZOJ 3670 NOI2014 动物园 KMP+dp

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3670 题意概述:令num[i]表示字符串由1~i的字符形成的前缀中不相重叠的相同前后缀的数 ...

  5. P2375 [NOI2014]动物园 KMP

    好,暴力能拿$50pts\space qwq$ 暴力的思路就是一直跳$nxt[j]$,直到它的长度小于串的一半,然后开始计数,当然要接着跳$nxt[j]$ 正解:考虑没有长度要求的(不要求不重合)公共 ...

  6. bzoj3670 [Noi2014]动物园——KMP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3670 第一次写KMP算法...又T又WA了半天... 1. num 数组表示包括其本身的前缀 ...

  7. 字符串(KMP):BZOJ 3670 [Noi2014]动物园

    3670: [Noi2014]动物园 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1521  Solved: 813[Submit][Status] ...

  8. BZOJ 3670: [Noi2014]动物园【KMP变形 】

    3670: [Noi2014]动物园 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 2738  Solved: 1475[Submit][Status ...

  9. BZOJ3670 [Noi2014]动物园 【KMP计数】

    3670: [Noi2014]动物园 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 3143  Solved: 1690 [Submit][Stat ...

随机推荐

  1. python封装发送邮件类

    import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart i ...

  2. Java_break和continue

    目录 Java_break Java_continue goto关键字 视频 Java_break break在任何循环语句的主题部分, 均可以用break强行退出循环, 不执行循环中的剩余语句 br ...

  3. swap函数模板

    在许多应用程序中,都有交换相同类型的两个变量内容的需要.例如,在对整数数组进行排序时,将需要一个函数来交换两个变量的值,如下所示: void swap(int &a, int &b) ...

  4. JavaScript中的??和?.和??=操作符

    JS中两种不常使用但挺实用的操作符:??和?. 一起来了解并学会使用它们吧: 空值合并操作符:?? 只有当操作符左侧为null或undefined时才会返回操作符右侧的值,否则返回左侧的值. eg: ...

  5. 线程安全性-原子性之synchronized锁

    原子性提供了互斥访问:同一时刻只能有一个线程进行操作: 除了Atomic包类之外,还有锁可以实现此功能: synchronized:  java关键字,依赖于jvm实现锁功能,被此关键字所修饰的,都是 ...

  6. ElasticSearch6.4.2

    做一个简单的API记录 1.依赖为6.4.2  比较老的版本 2.指定ES集群,可接多个Put(); Setting setting=Setting.builder().put("clust ...

  7. SQL Server导出MDF数据库文件

    更新日志 2022年6月13日 发布. 2022年6月2日 开始. 一句话总结:先分离,然后复制. 先分离要导出mdf数据库文件的数据库. 在Microsoft SQL Server Manageme ...

  8. IIS版本与Windows Server版本对应关系

    IIS 6.0随着Windows XP Professional 64位和Windows Server 2003发布. IIS 7.0随着Windows Vista和Windows Server 20 ...

  9. 17.Nginx 重写(location rewrite)

    Nginx 重写(location / rewrite) 目录 Nginx 重写(location / rewrite) 常见的nginx正则表达式 location lication的分类 loca ...

  10. BUUCTF-刷新过的图片

    刷新过的图片 刷新在MISC中比较特殊,一般是指F5隐写方式 直接使用工具提取出来,发现生成的是Pk开头的,应该是zip格式 使用16进制确认了是ZIP,将生成的output.txt改为output. ...