HDU 6153 A Secret (KMP)
题意:给定两个串,求其中一个串 s 的每个后缀在另一个串 t 中出现的次数。
析:首先先把两个串进行反转,这样后缀就成了前缀。然后求出 s 的失配函数,然后在 t 上跑一遍,如果发现不匹配了或者是已经完全匹配了,要计算,前面出现了多少个串的长度匹配也就是 1 + 2 + 3 + .... + j 在 j 处失配,然后再进行失配处理。注意别忘了,匹配完还要再加上最后的。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e6 + 100;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r > 0 && r <= n && c > 0 && c <= m;
} char s[maxn], t[maxn];
int f[maxn]; void getFail(){
f[0] = f[1] = 0;
for(int i = 1; i < n; ++i){
int j = f[i];
while(j && s[j] != s[i]) j = f[j];
f[i+1] = s[i] == s[j] ? j+1 : 0;
}
} int main(){
int T; cin >> T;
while(T--){
scanf("%s", t);
int len = strlen(t);
reverse(t, t + len);
scanf("%s", s);
n = strlen(s);
reverse(s, s + n);
getFail();
LL ans = 0;
int j = 0;
for(int i = 0; i < len; ++i){
while(j && s[j] != t[i]){
ans = (ans + (LL)j * (j+1LL) / 2) % mod;
j = f[j];
}
if(s[j] == t[i]) ++j;
if(j == n){
ans = (ans + (LL)j * (j+1LL) / 2 ) % mod;
j = f[j];
}
}
while(j){
ans = (ans + (LL)j * (j+1LL) / 2) % mod;
j = f[j];
}
printf("%I64d\n", ans);
}
return 0;
}
HDU 6153 A Secret (KMP)的更多相关文章
- 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6153 A Secret KMP,思维
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 题意:给了串s和t,要求每个t的后缀在在s中的出现次数,然后每个次数乘上对应长度求和. 解法:关 ...
- HDU 6153 A Secret ( KMP&&DP || 拓展KMP )
题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...
- HDU 6153 A Secret(扩展KMP模板题)
A Secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others) Total ...
- 【kmp或扩展kmp】HDU 6153 A Secret
acm.hdu.edu.cn/showproblem.php?pid=6153 [题意] 给定字符串A和B,求B的所有后缀在A中出现次数与其长度的乘积之和 A和B的长度最大为1e6 方法一:扩展kmp ...
- 2017中国大学生程序设计竞赛 - 网络选拔赛 1004 HDU 6153 A Secret (字符串处理 KMP)
题目链接 Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a presen ...
- HDU 6153 A Secret(扩展kmp)
A Secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total ...
- hdu 6153 思维+扩展kmp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 扩展kmp不理解的看下:http://www.cnblogs.com/z1141000271/p ...
- HDU 6153 A Secret 套路,求解前缀在本串中出现的次数
http://acm.hdu.edu.cn/showproblem.php?pid=6153 首先相当于翻转两个串,然后求s2前缀在s1中出现的次数. 这是一个套路啦 首先把两个串结合起来,中间加一个 ...
- HDU 6153 A Secret
A Secret Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)Total ...
随机推荐
- npm 自身的升级
npm 是随 NodeJS 一起发布的包管理工具,默认采用的并不一定是最新版本,某些情况下(比如使用了某个IDE)需要最新版本的 npm , 咋办?单独升级,官方的几种说法的操作比较复杂,本经验就是说 ...
- 20181110_wait和async
一. Awit和async的由来: await/async本身是一个语法糖,编译器提供的一个简化编程的功能; 在C#升级和.net Framework升级的时候, 产生的, 所以说并不是CLR的产物 ...
- select函数源码阅读
fd_set结构体 #undef __NFDBITS /* It's easier to assume 8-bit bytes than to get CHAR_BIT. */ #define __N ...
- java传递是引用的拷贝,既不是引用本身,更不是对象
java传递是引用的拷贝,既不是引用本身,更不是对象 2008-09-16 04:27:56| 分类: Java SE|举报|字号 订阅 下载LOFTER客户端 1. 简单类型是按值 ...
- Oracle数据库学习笔记(一)
Oracle的体系结构大体上分为两部分:Instance(实例)和Database(数据库). Instance(实例) :在Oracle Instance中主要包含了SGA以及一些进程(例如:P ...
- 新学一招,使用热门的Git
1.安装Git 1.本文所用版本为Git-1.8.0-preview20121022.exe , http://msysgit.googlecode.com/files/Git-1.8.0-prev ...
- golang代码覆盖率
写了自动化,就得统计代码覆盖率= = 原链接:https://www.elastic.co/blog/code-coverage-for-your-golang-system-tests 其实看了原链 ...
- 基于3D卷积神经网络的行为识别:3D Convolutional Neural Networks for Human Action Recognition
简介: 这是一片发表在TPAMI上的文章,可以看见作者有余凯(是百度的那个余凯吗?) 本文提出了一种3D神经网络:通过在神经网络的输入中增加时间这个维度(连续帧),赋予神经网络行为识别的功能. 相应提 ...
- day9-IO心得
Gevent协程 Select\Poll\Epoll异步IO与事件驱动 Python连接Mysql数据库操作 RabbitMQ队列 Redis\Memcached缓存 Paramiko SSH Tws ...
- Installation failed with message Failed to finalize session : INSTALL_FAILED_INVALID_APK: Split lib_slice_8_apk was defined multiple times. It is possible that this issue is resolved by uninstalling a
取消:Instant Run就行