Luogu2375 [NOI2014]动物园 (KMP)
写炸,上网,不同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)的更多相关文章
- BZOJ3670:[NOI2014]动物园(KMP)
Description 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学向游客要吃的,园长决定开设算法班,让动物们学习 ...
- BZOJ 3670: [Noi2014]动物园 [KMP]
求这玩意: 对于字符串S的前i个字符构成的子串,既是它的后缀同时又是它的前缀,并且该后缀与该前缀不重叠,将这种字符串的数量记作num[i] 对1,000,000,007取模的结果 n≤5,L≤1,00 ...
- [NOI2014]动物园(kmp)
题目 https://www.luogu.org/problemnew/show/P2375 做法 查找多少个前缀与后缀配对,其实就是\(fail\)树的深度 而不可重叠,其实\(i\)不可用的,\( ...
- BZOJ 3670 NOI2014 动物园 KMP+dp
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3670 题意概述:令num[i]表示字符串由1~i的字符形成的前缀中不相重叠的相同前后缀的数 ...
- P2375 [NOI2014]动物园 KMP
好,暴力能拿$50pts\space qwq$ 暴力的思路就是一直跳$nxt[j]$,直到它的长度小于串的一半,然后开始计数,当然要接着跳$nxt[j]$ 正解:考虑没有长度要求的(不要求不重合)公共 ...
- bzoj3670 [Noi2014]动物园——KMP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3670 第一次写KMP算法...又T又WA了半天... 1. num 数组表示包括其本身的前缀 ...
- 字符串(KMP):BZOJ 3670 [Noi2014]动物园
3670: [Noi2014]动物园 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1521 Solved: 813[Submit][Status] ...
- BZOJ 3670: [Noi2014]动物园【KMP变形 】
3670: [Noi2014]动物园 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2738 Solved: 1475[Submit][Status ...
- BZOJ3670 [Noi2014]动物园 【KMP计数】
3670: [Noi2014]动物园 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 3143 Solved: 1690 [Submit][Stat ...
随机推荐
- 【Windbg】记一次线程卡主的问题
测试告诉我们定时任务没有执行了,排查相关日志,只有开始执行,没有执行结束.估计是什么地方卡主了. 所以dump分析日志 先检查一下加载情况 !eeversion 线程卡主了,先看线程 !runaway ...
- 关于JNPF3.4版本的三大改变,你真的了解了吗?
- 图解 Apache SkyWalking UI 的使用
Apache SkyWalking的UI界面主要分为以下几个区域: 功能选择区:这里列出了主要的UI功能,包括仪表盘.拓扑图.追踪.性能刨析.告警等功能 重新加载区:控制重新加载机制,包括定期重新加载 ...
- 优先队列STL
引入 优先队列是一种特殊的队列,它的功能是--自动排序. 基本操作: q.size(); //返回q里元素个数 q.empty(); //返回q是否为空,空则返回1,否则返回0 q.push(k); ...
- 蓝牙、WiFi、ZigBee三大无线通信技术协议模块哪一个是最好的?
曾经,在2015年极客公园创新大会上,小米首次在非官方平台发布了新款产品小米智能家庭套装.自此,Zigbee便常出现在大众视野中. 如今,小米在IoT物联网应用开发者平台上明确说明,不再推广Zigbe ...
- Kubernetes将弃用Docker!与 containerd容器引擎
时间戳:2022-06-07 20:32:19 星期二 撰写文档参考:(阿良-腾讯课堂)Kubernetes将弃用Docker 参考博客k8s入坑之路(3)containerd容器 container ...
- 深度学习与计算机视觉教程(15) | 视觉模型可视化与可解释性(CV通关指南·完结)
作者:韩信子@ShowMeAI 教程地址:http://www.showmeai.tech/tutorials/37 本文地址:http://www.showmeai.tech/article-det ...
- 开发工具-在线JSON相关的工具
更新记录: 2022年6月7日 新增链接. 2022年6月1日 开始. https://www.sojson.com/json2entity.html URL参数互转JSON https://tool ...
- 记住这几个git命令就够了
git clone: 下载初始化git add:添加git commit -m ' ' :提交 带消息git push:推送git pull: 拉取 git config --global user. ...
- Ubuntu系统iptables安全防护整改计划
端口开放 默认防火墙是开放所有端口的,如果拿来做应用服务器,就很危险,所以要把防火墙用起来,只将需要的端口开放,ubuntu用的是iptables防火墙. iptables处理流程 iptables ...