UVALive 5876 Writings on the Wall
来源:互联网 发布:水妖钢笔 知乎 编辑:程序博客网 时间:2024/06/16 18:49
这道题数据很水,用哈希和暴力都可以水过去。
不过正解很巧妙,类似于HDU6096用ac自动机水过去的方法。
题目给出两个字符串sa和sb,于是构造一个sb+’?’+sa的字符串,对其跑KMP的失配函数,这样对于这个字符串结尾的所有的失配都是答案。
代码:
#include<bits/stdc++.h>using namespace std;const int N = 5e4+5;char s1[N], s2[2*N];int f[2*N];void solve(char *s) { int l = strlen(s), ans = 0; f[0] = f[1] = 0; for(int i = 1; i < l; i++) { int j = f[i]; while(j && s[i]!=s[j]) j = f[j]; f[i+1] = s[i]==s[j]?j+1:0; } while(l) { ans++; l = f[l]; } printf("%d\n", ans);}int main() { int T; scanf("%d", &T); while(T--) { scanf("%s%s", s1+1, s2); s1[0] = '?'; strcat(s2, s1); solve(s2); } return 0;}
阅读全文
0 0
- UVALive 5876 - Writings on the Wall 【KMP】
- UVALive 5876 Writings on the Wall
- kmp Writings on the Wall Virtual Judge5876
- Cross the Wall UVALive
- UVALive 3181 Fixing the Great Wall
- UVALive - 5097 Cross the Wall(斜率优化)
- Across the wall on linux
- The Handwriting on the Cheese Wall
- 短语:the writing is on the wall
- 2405. Mirror, Mirror on the Wall
- Date on Database: Writings 2000-2006
- 做一只墙上的苍蝇(a fly on the wall)
- Who Has The Best Tech Department On Wall Street?
- SDUT——Mirror, Mirror on the Wall 解题报告
- Pink Floyd -- The Wall
- hdu2124 Repair the Wall
- hdu1543 Paint the Wall
- hdu1543 Paint the Wall
- HDU
- Java学习笔记——基础知识:成员变量和局部变量的区别
- 图像分割“Efficient Deep Models for Monocular Road Segmentation”
- 我所理解的委托和匿名函数
- 翻译《有关编程、重构及其他的终极问题?》——31.在C-C++中数组不是值传递的
- UVALive 5876 Writings on the Wall
- RocketMQ(四)——消息重试
- Oracle中varchar,varchar2,nvarchar,nvarchar2的区别
- Mongo---配置多数据源
- 什么是梯度下降法?????
- 小白学tkinter(Canvas组件(画布))
- kaldi中的深度神经网络
- Java并发编程(3)——synchronized
- 扫地机器人行走路线