xtu字符串 C. Marlon's String
C. Marlon's String
64-bit integer IO format: %lld Java class name: Main
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 解题:与扩展KMP无关,与前缀有关。。。直接KMP
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int fail[];
char pa[],s[];
int a[],b[];
void kmp(int *arr){
int i,j,k;
fail[] = fail[] = ;
for(i = ; pa[i]; i++){
j = fail[i];
while(j && pa[i] != pa[j]) j = fail[j];
fail[i+] = pa[j] == pa[i] ? j+:;
}
for(j = i = ; s[i]; i++){
while(j && s[i] != pa[j]) j = fail[j];
if(pa[j] == s[i]){
arr[++j]++;
}
}
for(i = strlen(pa); i >= ; i--)
if(fail[i]) arr[fail[i]] += arr[i];
}
int main(){
int t,i,len;
LL ans;
scanf("%d",&t);
while(t--){
scanf("%s %s",s,pa);
memset(a,,sizeof(a));
memset(b,,sizeof(b));
kmp(a);
reverse(s,s+strlen(s));
reverse(pa,pa+strlen(pa));
kmp(b);
ans = ;
for(len = strlen(pa),i = ; i < len; i++)
ans += (LL)a[i]*b[len-i];
printf("%lld\n",ans);
}
return ;
}
xtu字符串 C. Marlon's String的更多相关文章
- C字符串和C++中string的区别 &&&&C++中int型与string型互相转换
在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别: C字符串 string对象(C++) 所需的头文件名称 ...
- C字符串和C++中string的区别 &&&&C++中int型与string型互相转换
在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别: C字符串 string对象(C++) 所需的头文件名称 ...
- 常用类一一字符串相关类一一String类 字符串的使用
Java字符串就是Unicode字符序列,例如“Java”就是4个Unicode字符J,a,v,a组成的. Java没有内置的字符串类型,而是在标准Java类库中提供了一个预定义的类String,每个 ...
- Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty
原文:Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty public class NullToEmptyStringResolver : De ...
- C语言判断字符串是否是 hex string的代码
把写内容过程中经常用到的一些内容段备份一下,如下内容内容是关于C语言判断字符串是否是 hex string的内容. { static unsigned int hex2bin[256]={0}; me ...
- 011-JSON、JSONObject、JSONArray使用、JSON数组形式字符串转换为List<Map<String,String>>的8种方法
一.JSON数据格式 1.1.常用JSON数据格式 1.对象方式:JSONObject的数据是用 { } 来表示的, 例如: { "id" : "123", & ...
- Java String类相关知识梳理(含字符串常量池(String Pool)知识)
目录 1. String类是什么 1.1 定义 1.2 类结构 1.3 所在的包 2. String类的底层数据结构 3. 关于 intern() 方法(重点) 3.1 作用 3.2 字符串常量池(S ...
- Java中的字符串操作(比较String,StringBuiler和StringBuffer)
一.前言 刚开始学习Java时,作为只会C语言的小白,就为其中的字符串操作而感到震撼.相比之下,C语言在字节数组中保存一个结尾的\0去表示字符串,想实现字符串拼接,还需要调用strcpy库函数或者自己 ...
- C字符串和C++中string的区别
在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别: C字符串 string对象(C++) 所需的头文件名称 ...
随机推荐
- JavaScript-获取当前元素的相关元素或节点--方法总结
1.获取当前元素中的第一个子节点 document.getElementById("uu").firstChild 2.获取当前元素中的第一个子元素 document.getEle ...
- [在读]javascript框架设计
司徒正美的书,内容我觉得不错,国内的书很少会讲这些.当然也有很多人吐槽它只贴代码没有解释,文笔不够优美啥啥的,我想说,不要在意这些细节,反正是值得买的一本.
- 几种创建线程方式Thread类和Runnable接口
对于很多想学习java的人来说,经常听别人说线程难,其实真正理解了线程后,一点都不会觉得线程难,这里我为大家梳理下线程的创建方式吧. 一.线程的创建方式有三种 1.继承Thread类 2.实现Runn ...
- poj1857 To Europe! To Europe!
思路: 一维dp. 实现: #include <cstdio> #include <iostream> using namespace std; const int INF = ...
- 洛谷P2742 【模板】二维凸包
题意 求凸包 Sol Andrew算法: 首先按照$x$为第一关键字,$y$为第二关键字从小到大排序,并删除重复的点 用栈维护凸包内的点 1.把$p_1, p_2$放入栈中 2.若$p_{i{(i & ...
- jQuery选择器之表单元素选择器
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...
- IDEA下MyBatis错误总结
1. Pom.xml配置 语法顺序 <properties resource="config.properties"> </properties> < ...
- iOS 网络开发
http://www.cnblogs.com/kenshincui/p/4042190.html
- iOS开发中的HTML解析
在进行解析前,先将下面的第三方类添加到工程中: 添加以上三个类必须添加一个库,这个库是:libxml2.2.dylib. 还需要设置一些路径参数这个路径的设置,在 targets中,在build se ...
- 通过HA方式操作HDFS
之前操作hdfs的时候,都是固定namenode的地址,然后去操作.这个时候就必须判断namenode的状态为active还是standby,比较繁琐,如果集群使用了HA的形式,就很方便了 直接上代码 ...