题目链接

给一个字符串s, n个字符串str。 令tmp为s中不重叠的两个连续子串合起来的结果, 顺序不能改变。问tmp能形成n个字符串中的几个。

初始将一个数组dp赋值为-1。

对str做kmp, 然后与串s进行匹配, 看哪些长度的串可以匹配到, 比如说匹配到了长度为j的串, 那么dp[j] = i, i是此时串s的位置。 然后将s和str都反转, 在做一次kmp, 在进行匹配, 对于匹配到的长度j, 看dp[lenStr-j]是否为-1, 如果不为-1, 看lenS-i是否大于等于dp[lenStr-j], 意思是这一部分在之前那一部分的右边。

语言表达能力好差................................感觉根本没有说清.还是看代码

 #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
string s, str;
int nextt[], dp[];
void initKmp() {
nextt[] = -;
int i = , j = -, len = str.size();
while(i<len) {
if(j==-||str[i]==str[j])
nextt[++i] = ++j;
else
j = nextt[j];
}
}
int solve() {
cin>>str;
if(str.size()<=)
return ;
initKmp();
mem1(dp);
int i = , j = , len1 = s.size(), len2 = str.size();
while(i<len1) {
if(j==-||s[i]==str[j]) {
i++, j++;
} else {
j = nextt[j];
}
if(j == len2)
return ;
if(j>=&&dp[j]==-)
dp[j] = i;
}
reverse(s.begin(), s.end());
reverse(str.begin(), str.end());
initKmp();
i = , j = ;
while(i<len1) {
if(j == - || s[i]==str[j]) {
i++, j++;
} else {
j = nextt[j];
}
if(j>=&&dp[len2-j]!=- && dp[len2-j]<=len1-i) {
reverse(s.begin(), s.end());
return ;
}
}
reverse(s.begin(), s.end());
return ;
}
int main()
{
cin>>s;
int n, ans = ;
cin>>n;
for(int i = ; i<n; i++) {
ans += solve();
}
cout<<ans<<endl;
return ;
}

codeforces 149E . Martian Strings kmp的更多相关文章

  1. CodeForces 149E Martian Strings exkmp

    Martian Strings 题解: 对于询问串, 我们可以从前往后先跑一遍exkmp. 然后在倒过来,从后往前跑一遍exkmp. 我们就可以记录下 对于每个正向匹配来说,最左边的点在哪里. 对于每 ...

  2. Codeforces 177G2 Fibonacci Strings KMP 矩阵

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF117G2.html 题目传送门 - CF177G2 题意 定义斐波那契字符串如下: $s_1="a ...

  3. Codeforces149E - Martian Strings(KMP)

    题目大意 给定一个字符串T,接下来有n个字符串,对于每个字符串S,判断是否存在T[a-b]+T[c-d]=S(1 ≤ a ≤ b < c ≤ d ≤ length(T)) 题解 对于每个字符串S ...

  4. CF 149E Martian Strings 后缀自动机

    这里给出来一个后缀自动机的题解. 考虑对 $s$ 的正串和反串分别建后缀自动机. 对于正串的每个节点维护 $endpos$ 的最小值. 对于反串的每个节点维护 $endpos$ 的最大值. 这两个东西 ...

  5. Codeforces 149 E. Martian Strings

    正反两遍扩展KMP,维护公共长度为L时.出如今最左边和最右边的位置. . .. 然后枚举推断... E. Martian Strings time limit per test 2 seconds m ...

  6. xtu summer individual-4 D - Martian Strings

    Martian Strings Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  7. POJ 2406 Power Strings (KMP)

    Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...

  8. poj 2406 Power Strings kmp算法

    点击打开链接 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27368   Accepted:  ...

  9. Power Strings(kmp妙解)

    Power Strings Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tota ...

随机推荐

  1. DOM元素对象的属性和方法(2)

    11.contentEditable 作用:设置或返回元素内容可否编辑布尔值,HTML5新增属性 <!DOCTYPE html> <html> <head> < ...

  2. SqlServer导库语句

    GO /****** 对象: StoredProcedure [dbo].[sp_ExportDatabase] 脚本日期: 07/18/2013 12:37:26 ******/ SET ANSI_ ...

  3. LocalStorage存储

     1.localStorage存储服务: .factory('storageSvc', [function () { return { //保存数据 save: function (key, valu ...

  4. OC 实现多选参数

    在iOS的开发过程中有许多方法都是有可选参数的,例如: + (instancetype)arrayWithObjects:(ObjectType)firstObj, ... NS_REQUIRES_N ...

  5. Linux(CentOS6.4、CentOS6.3)下安装、配置PostgreSQL9.2

    首先,卸载机器上默认安装的PostgreSQL-8.3.x . [root@localhost ~]# rpm -qa | grep postgresql postgresql-jdbc--.el6. ...

  6. oracle中查找执行效率低下的SQL

    v$sqltext:存储的是完整的SQL,SQL被分割 v$sqlarea:存储的SQL 和一些相关的信息,比如累计的执行次数,逻辑读,物理读等统计信息(统计) v$sql:内存共享SQL区域中已经解 ...

  7. swift和objc之對比

    http://mobidev.biz/blog/swift_how_we_code_your_ios_apps_twice_faster

  8. Scala中的apply实战详解

    apply可以应用与Object和Class,单调用情景不一样. 通过Array(1,2,3,4,5) 可以调用到Array中的.appy方法.你可以看源码是如何实现apply的. 类中的appy比较 ...

  9. Sencha Touch 之 Ext.ComponentManager.get方法使用

    HTML代码: <!doctype html> <html> <head> <meta charset="utf-8"> <t ...

  10. J2SE知识点摘记(二十四)

     覆写hashCode() 在明白了HashMap具有哪些功能,以及实现原理后,了解如何写一个hashCode()方法就更有意义了.当然,在HashMap中存取一个键值对涉及到的另外一个方法为equa ...