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”对于每个 ...
随机推荐
- 如何判断自己IP是内网IP还是外网IP
tcp/ip协议中,专门保留了三个IP地址区域作为私有地址,其地址范围如下: 10.0.0.0/8:10.0.0.0-10.255.255.255 172.16.0.0/12:172.16.0.0- ...
- Scrapy框架使用代理
使用代理抓取https://www.baidu.com/s?wd=ip # -*- coding: utf-8 -*- ''' 一.在settings.py中开启中间件 DOWNLOADER_MIDD ...
- tar---打包,解压缩linux的文件和目录
tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件.tar最初被用来在磁带上创建档案,现在,用户可以在 ...
- HDU——T 1506 Largest Rectangle in a Histogram|| POJ——T 2559 Largest Rectangle in a Histogram
http://acm.hdu.edu.cn/showproblem.php?pid=1506 || http://poj.org/problem?id=2559 Time Limit: 2000/1 ...
- You have ettempted to queue to many files.You may select one files.
<script type="text/javascript" src="/script/swfupload/swfupload.js"></s ...
- 使用 Facebook开源动画库 POP 实现真实衰减动画
1. POP动画基于底层刷新原理.是基于CADisplayLink,1秒钟运行60秒,接近于游戏开发引擎 @interface ViewController () @property (nonatom ...
- .Net接口调试与案例
1.通过查看日志,可以看出问题的原因. 2.断点调试. 3.本地测试,确保无误后,线上测试. 4.输出测试. 通过get的方式,测试接口. // [HttpPost] public ActionRes ...
- Elasticsearch之shield(权限)插件安装之后的浏览详解
前期博客 Elasticsearch-2.4.3的3节点安装(多种方式图文详解)(含 head.kopf.marvel.shield和watcher插件安装和使用) 访问es:-u es_admin ...
- API集合开发文档
百度翻译api https://www.cnblogs.com/DevilX5/p/7079470.html 实现QQ第三方登录.网站接入 http://blog.csdn.net/u01067894 ...
- Python爬虫之『urlopen』
本文以爬取百度首页为示例来学习,python版本为python3.6.7,完整代码会在文章末附上 本次学习所用到的python框架:urllib.request 本次学习所用到的函数: urllib. ...