题意:给定两个串,求其中一个串 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)的更多相关文章

  1. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6153 A Secret KMP,思维

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 题意:给了串s和t,要求每个t的后缀在在s中的出现次数,然后每个次数乘上对应长度求和. 解法:关 ...

  2. HDU 6153 A Secret ( KMP&&DP || 拓展KMP )

    题意 : 给出两个字符串,现在需要求一个和sum,考虑第二个字符串的所有后缀,每个后缀对于这个sum的贡献是这个后缀在第一个字符串出现的次数*后缀的长度,最后输出的答案应当是 sum % 1e9+7 ...

  3. HDU 6153 A Secret(扩展KMP模板题)

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others) Total ...

  4. 【kmp或扩展kmp】HDU 6153 A Secret

    acm.hdu.edu.cn/showproblem.php?pid=6153 [题意] 给定字符串A和B,求B的所有后缀在A中出现次数与其长度的乘积之和 A和B的长度最大为1e6 方法一:扩展kmp ...

  5. 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 ...

  6. HDU 6153 A Secret(扩展kmp)

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total ...

  7. hdu 6153 思维+扩展kmp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6153 扩展kmp不理解的看下:http://www.cnblogs.com/z1141000271/p ...

  8. HDU 6153 A Secret 套路,求解前缀在本串中出现的次数

    http://acm.hdu.edu.cn/showproblem.php?pid=6153 首先相当于翻转两个串,然后求s2前缀在s1中出现的次数. 这是一个套路啦 首先把两个串结合起来,中间加一个 ...

  9. HDU 6153 A Secret

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total ...

随机推荐

  1. Window app遇到的问题

    Windows下两个应用之间进行UDP通讯,必须要先互相发送过数据,才能收到其他的数据.解决方法: <Capabilities> <Capability Name="int ...

  2. 【BZOJ】1823: [JSOI2010]满汉全席(2-sat)

    题目 传送门:QWQ 分析 2-sat模板(然而辣鸡如我还是调了好久) 代码 //bzoj 1823 2-sat #include <bits/stdc++.h> using namesp ...

  3. PCI、PCI-x,PCI-E兼容以及他们之间的区别详细图解

    一.PCI PCI接口分为32bit和64bit两种,32bit就是一般台式机使用的普通的pci接口(图一.图三),64bit接口比32bit接口长一些一般只出现在服务器上(图四.图五).32bit和 ...

  4. [Python] Request module

    http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

  5. leetcode289

    public class Solution { public void GameOfLife(int[][] board) { ) - ; ].GetLength() - ; var list = n ...

  6. STL : 反向迭代器(Reverse Iterator)

    1. 定义反向迭代器(Reverse Iterator)是一种反向遍历容器的迭代器.也就是,从最后一个元素到第一个元素遍历容器.反向迭代器将自增(和自减)的含义反过来了:对于反向迭代器,++运算将访问 ...

  7. Echart ---超详细介绍

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. vs code 配置spring boot开发环境

    一.环境变量 jdk环境变量一键设置 管理員运行 - 一支小白 - 博客园https://www.cnblogs.com/startnow/p/7416533.html 二.安装插件 1.Java E ...

  9. Python基础:面向对象基础 (一) 类及其属性和魔法方法

    定义类,添加和获取对象属性 # 定义类 格式如下 # class 类名: # 方法列表 # 新式类定义形式 # info 是一个实例方法,第一个参数一般是self,表示实例对象本身 class Her ...

  10. IDEA04 工具窗口管理、各种跳转、高效定位、行操作、列操作、live template、postfix、alt enter、重构、git使用

    1 工具窗口管理 所有的窗口都是在view -> tools windows 下面的,这些窗口可以放在IDEA的上下左右各个位置:右键某个窗口后选择move to 即可进行位置调整 2 跳转 2 ...