题意:给定两个串,求其中一个串 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. Centos 文件查找命令

    find [搜索范围] [搜索条件] #搜索文件 find / -name install.log #避免大范围搜索,会非常耗费系统资源 #find是在系统当中搜索符合条件的文件名.如果需要匹配,使用 ...

  2. enq:TM-contention

    enq:TM-contention 2011-08-04 15:55:17 分类: Linux 7.1 enq:TM-contention         执行dml期间,为防止对与dml相关的对象进 ...

  3. Oracle 热备份

    Oracle 热备份是指数据库处于open状态下,对数据库的数据文件.控制文件.参数文件.密码文件等进行一系列备份操作. 热备是基于用户管理备份恢复的一种方式,也是除了RMAN备份之外较为常用的一种备 ...

  4. Java 编译???

    如果是在命令行下,编译就是 javac a.java 如果有错误,那么命令运行之后会显示错误 但是在eclipse下,我都是直接点击运行按钮的,如果有错误,在编程是就提示了,那么是怎么编译的呀 大的工 ...

  5. 记:cloudstack--gluster主存储上的一个文件损坏导致SSVM启动失败

    cloudstack的系统vm(ssvm不停的重建失败).- 1.cloudstack-management 的关键日志 这行 cannot read header 'mnt.......':Inva ...

  6. C# 命名空间(Namespace)

    命名空间的设计目的是提供一种让一组名称与其他名称分隔开的方式.在一个命名空间中声明的类的名称与另一个命名空间中声明的相同的类的名称不冲突. 也即是不同的命名空间可以有相同的类名

  7. 论XGBOOST科学调参

    XGBOOST的威力不用赘述,反正我是离不开它了. 具体XGBOOST的原理可以参见之前的文章<比XGBOOST更快--LightGBM介绍> 今天说下如何调参. bias-varianc ...

  8. Swift 动画片段

    UIView.transitionWithView( self.WeatherDetailsView, duration: 0.7, options: .TransitionCrossDissolve ...

  9. 1、svn架设、基本命令

    SVN是Subversion的简称,是一个开放源代码的版本控制系统.是一项十分基础,必须能够熟练使用的工具.Apache网站:https://subversion.apache.org/ 采用C/S模 ...

  10. An Intuitive Explanation of Fourier Theory

    Reprinted from: http://cns-alumni.bu.edu/~slehar/fourier/fourier.html An Intuitive Explanation of Fo ...