ZOJ 题目3587 Marlon's String(KMP)
Marlon's String
Time Limit: 2 Seconds
Memory Limit: 65536 KB
Long long ago, there was a coder named Marlon. One day he picked two string on the street.A problem suddenly crash his brain...
Let Si..j denote the i-th character to the
j-th character of string S.
Given two strings S and T. Return the amount of tetrad (a,b,c,d) which satisfy
Sa..b + Sc..d = T , a≤b and
c≤d.
The operator + means concate the two strings into one.
Input
The first line of the data is an integer Tc.Following Tc test cases, each contains two line. The first line is
S. The second line is T.The length of S and
T are both in range [1,100000]. There are only letters in string S and T.
Output
For each test cases, output a line for the result.
Sample Input
1
aaabbb
ab
Sample Output
9
题目大意:Return the amount of tetrad (a,b,c,d) which satisfy
Sa..b + Sc..d = T , a≤b and
c≤d.
ac代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[200010],b[200010];
int c1[200010],c2[200010];
int next[200010];
void getnext(char *a)
{
next[0]=next[1]=0;
int i;
int m=strlen(a);
for(i=1;i<m;i++)
{
int j=next[i];
while(j&&a[i]!=a[j])
j=next[j];
next[i+1]=(a[i]==a[j])?j+1:0;
}
}
void kmp(char *a,char *b,int *c)
{
getnext(b);
int j=0,i;
int n=strlen(a);
int m=strlen(b);
for(i=0;i<n;i++)
{
while(j&&a[i]!=b[j])
j=next[j];
if(a[i]==b[j])
{
j++;
c[j]++;
}
}
for(i=m;i>=0;i--)
if(next[i])
c[next[i]]+=c[i];
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i;
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
scanf("%s%s",a,b);
int len1=strlen(a);
int len2=strlen(b);
kmp(a,b,c1);
reverse(a,a+len1);
reverse(b,b+len2);
kmp(a,b,c2);
long long ans=0;
for(i=0;i<len2;i++)
ans+=(long long)c1[i]*c2[len2-i];
printf("%lld\n",ans);
}
}
ZOJ 题目3587 Marlon's String(KMP)的更多相关文章
- ZOJ 3587 Marlon's String 扩展KMP
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3587 题意:给出两个字符串S和T.S,T<=100000.拿出 ...
- ZOJ题目分类
ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...
- hdu3336 Count the string kmp+dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3336 很容易想到用kmp 这里是next数组的应用 定义dp[i]表示以s[i]结尾的前缀的总数 那么 ...
- 翻转子串(string+KMP+程序猿面试金典)
翻转子串 參与人数:1197时间限制:3秒空间限制:32768K 通过比例:35.03% 最佳记录:0 ms|8552K(来自 ) 题目描写叙述 假定我们都知道很高效的算法来检查一个单词是否为其它字符 ...
- hdu 3336 Count the string KMP+DP优化
Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...
- hdoj 5311 Hidden String(KMP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5311 思路分析:该问题要求在字符串中是否存在三个不相交的子串s[l1..r1], s[l2..r2], ...
- HDU 3336 Count the string KMP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3336 如果你是ACMer,那么请点击看下 题意:求每一个的前缀在母串中出现次数的总和. AC代码: # ...
- 【XSY2472】string KMP 期望DP
题目大意 给定一个由且仅由字符'H','T'构成的字符串\(S\). 给定一个最初为空的字符串\(T\) ,每次随机地在\(T\)的末尾添加'H'或者'T'. 问当\(S\)为\(T\)的后缀时, ...
- Count the string kmp
问题描述众所周知,aekdycoin擅长字符串问题和数论问题.当给定一个字符串s时,我们可以写下该字符串的所有非空前缀.例如:S:“ABAB”前缀是:“A”.“AB”.“ABA”.“ABAB”对于每个 ...
随机推荐
- Mac OSX 平台安装 MongoDB
Mac OSX 平台安装 MongoDB MongoDB 提供了 OSX 平台上 64 位的安装包,你可以在官网下载安装包. 下载地址:https://www.mongodb.com/download ...
- 【Linux】JDK+Eclipse 搭建C/C++开发环境
注:本文所提供的参考示例是在CentOS Linux环境下的安装,不保证适用于其他版本的Linux系统. · 安装前的注意事项 编译源代码是需要对应的代码编译工具的,本文中安装的Eclipse只 ...
- 30.angularJS第一个实例
转自:https://www.cnblogs.com/best/tag/Angular/ AngularJS 通过 ng-directives 扩展了 HTML. ng-app 指令定义一个 Angu ...
- codeforces 710E Generate a String(简单dp)
传送门:http://codeforces.com/problemset/problem/710/E 分析: 让你写一个全由"a"组成的长为n的串,告诉你两种操作,第一种:插入一个 ...
- XML结构,写到TreeView树上
http://blog.csdn.net/ztzi321/article/details/44077563
- git hub在windows上的使用
每次使用都需要查找,太麻烦了. 转过来自己用! git hub在windows上的使用 一. 首先安装客户端 Git for Windows. 按提示默认安装后,进入git bash(桌面上) 首先检 ...
- 一种基于RGB空间的对照度增强的filter
今天看前辈博客的时候看到一种新的基于RGB颜色空间的image contrast enhance filter 流浪的鱼link: http://blog.csdn.net/jia20003/arti ...
- SAP学习之路
此贴记录学习SAP过程~如有错误请指出~ 6.看着网上学习SAP的貌似比較花精力.学习好的话发展前景还是不错的 5.尽管还不是非常懂.可是还是充满期待,期待着java转型abap. 能够在虚拟机上安装 ...
- 带你彻彻底底弄懂Scroller
Scroller的使用 这是一个滑动帮助类.并不能够使View真正的滑动,而是依据时间的流逝.获取插值器中的数据.传递给我们.让我们去配合scrollTo/scrollBy去让view产生缓慢滑动,产 ...
- PHP CLI模式下的多进程应用分析
PHP在非常多时候不适合做常驻的SHELL进程, 他没有专门的gc例程, 也没有有效的内存管理途径. 所以假设用PHP做常驻SHELL, 你会常常被内存耗尽导致abort而unhappy 并且, 假设 ...