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”对于每个 ...
随机推荐
- orm 通用方法——DeleteModel 主键删除
定义代码: /** * 描述:删除对象 * 作者:Tianqi * 日期:2014-09-17 * param:model 对象实例,包含主键 * return:int 受影响行数 * return: ...
- Sub Thread to update main Thread (UI)
Sub Thread to update main Thread (UI) main Thread : A has Hander.HandleMessage() to process the & ...
- 手把手教你在VMware虚拟机中安装Ubuntu14.04系统
在VMware中创建完虚拟机之后,一般需要给虚拟机安装系统,比较受青睐的系统有Ubuntu和Centos,关于Centos系统的安装之前已经写过了,感兴趣的小伙伴可以戳这篇文章:靠谱的centos7. ...
- Flex4之事件详解
第一.Flex事件简介 事件贯穿于Flex应用开发的全过程.事件是ActionScript .0中最重要的部分之一,也是Flex应用程序开发的核心基础.本章将在DOM 3的基础上详细讲解ActionS ...
- 网络场景图WebScene介绍
网络场景图的创建同样需要ID,然后再放入3维的地图底图中. 1.创建一个新的网络场景图 "esri/WebScene" var scene = new WebScene({ por ...
- IT人都欠自已一个Lable Page
平时, 你是不是喜欢将一些工作或者技术的网站都会Mark一下.慢慢,自已收藏的标签越来越多.收藏后去查找的比较麻烦,而且不是很方便. 下面的文章是介绍我自已是怎么实现一个简单的Lable Page. ...
- 【Henu ACM Round#14 D】Kefa and Dishes
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 状态压缩动规. 可以写成记忆化搜索的形式. f[bit][p] 表示选取的菜的情况为bit(用0..2^(N)-1的二进制形式表示各 ...
- codevs——T1006 等差数列
http://codevs.cn/problem/1006/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descr ...
- 洛谷 P3670 [USACO17OPEN]Bovine Genomics S奶牛基因组(银)
P3670 [USACO17OPEN]Bovine Genomics S奶牛基因组(银) 题目描述 Farmer John owns NN cows with spots and NN cows wi ...
- UITableViewCell的prepareForReuse方法
cell被重用怎样提前知道? 重写cell的prepareForReuse官方头文件里有说明.当前已经被分配的cell假设被重用了(一般是滚动出屏幕外了),会调用cell的prepareForReus ...