Martian Strings

Time Limit: 2000ms
Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 149E
64-bit integer IO format: %I64d      Java class name: (Any)

 

During the study of the Martians Petya clearly understood that the Martians are absolutely lazy. They like to sleep and don't like to wake up.

Imagine a Martian who has exactly n eyes located in a row and numbered from the left to the right from 1 to n. When a Martian sleeps, he puts a patch on each eye (so that the Martian morning doesn't wake him up). The inner side of each patch has an uppercase Latin letter. So, when a Martian wakes up and opens all his eyes he sees a string sconsisting of uppercase Latin letters. The string's length is n.

"Ding dong!" — the alarm goes off. A Martian has already woken up but he hasn't opened any of his eyes. He feels that today is going to be a hard day, so he wants to open his eyes and see something good. The Martian considers only m Martian words beautiful. Besides, it is hard for him to open all eyes at once so early in the morning. So he opens two non-overlapping segments of consecutive eyes. More formally, the Martian chooses four numbers abcd, (1 ≤ a ≤ b < c ≤ d ≤ n) and opens all eyes with numbers i such that a ≤ i ≤ b or c ≤ i ≤ d. After the Martian opens the eyes he needs, he reads all the visible characters from the left to the right and thus, he sees some word.

Let's consider all different words the Martian can see in the morning. Your task is to find out how many beautiful words are among them.

 

Input

The first line contains a non-empty string s consisting of uppercase Latin letters. The strings' length is n (2 ≤ n ≤ 105). The second line contains an integer m (1 ≤ m ≤ 100) — the number of beautiful words. Next m lines contain the beautiful words pi, consisting of uppercase Latin letters. Their length is from 1 to 1000. All beautiful strings are pairwise different.

 

Output

Print the single integer — the number of different beautiful strings the Martian can see this morning.

 

Sample Input

Input
ABCBABA
2
BAAB
ABBA
Output
1

Hint

Let's consider the sample test. There the Martian can get only the second beautiful string if he opens segments of eyes a = 1, b = 2 and c = 4, d = 5 or of he opens segments of eyes a = 1, b = 2 and c = 6, d = 7.

 

Source

 
 
解题:KMP,几天没写KMP,居然犯了个低级错误!题意大概就是给出一个母串,再给出若干字串,要求统计有多少字串可以优母串中的两个字串拼接而成!注意顺序,样例说得很清楚了,左边的一定要在左边。假设a<b<c<d,abcd这样拼接是可以的,而cdab是禁止的。注意母串逆转过去后,在下次操作前,要逆转回去。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
char str[],s[];
int n,fail[],mark[];
void getFail(char *s){
int i,j;
fail[] = fail[] = ;
for(i = ; s[i]; i++){
j = fail[i];
while(j && s[i] != s[j]) j = fail[j];
fail[i+] = s[i] == s[j]?j+:;
}
}
void kmp(char *str,char *p){
memset(mark,-,sizeof(mark));
int i,j;
for(i = j = ; str[i]; i++){
while(j && str[i] != p[j]){j = fail[j];}
if(str[i] == p[j]) j++;
if(j && mark[j] == -) mark[j] = i;
}
}
bool Find(char *str,char *p){
int i,j,len = strlen(p),slen = strlen(str);
for(i = j = ; str[i]; i++){
while(j && str[i] != p[j]) j = fail[j];
if(str[i] == p[j]) j++;
if(j && mark[len-j] != - && mark[len-j] < slen-i-)
return true;
}
return false;
}
int main(){
int ans;
while(~scanf("%s",str)){
scanf("%d",&n);
ans = ;
while(n--){
scanf("%s",s);
getFail(s);
kmp(str,s);
reverse(str,str+strlen(str));
reverse(s,s+strlen(s));
getFail(s);
if(Find(str,s)) ans++;
reverse(str,str+strlen(str));
}
printf("%d\n",ans);
}
return ;
}

xtu summer individual-4 D - Martian Strings的更多相关文章

  1. Codeforces 149 E. Martian Strings

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

  2. CodeForces 149E Martian Strings exkmp

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

  3. xtu summer individual 2 C - Hometask

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

  4. Codeforces149E - Martian Strings(KMP)

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

  5. codeforces 149E . Martian Strings kmp

    题目链接 给一个字符串s, n个字符串str. 令tmp为s中不重叠的两个连续子串合起来的结果, 顺序不能改变.问tmp能形成n个字符串中的几个. 初始将一个数组dp赋值为-1. 对str做kmp, ...

  6. xtu summer individual 4 C - Dancing Lessons

    Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  7. xtu summer individual 3 C.Infinite Maze

    B. Infinite Maze time limit per test  2 seconds memory limit per test  256 megabytes input standard ...

  8. xtu summer individual 2 E - Double Profiles

    Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  9. xtu summer individual 1 A - An interesting mobile game

    An interesting mobile game Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on H ...

随机推荐

  1. Tree CodeForces -932D

    错误记录:如下注释语句 #include<cstdio> #include<algorithm> using namespace std; typedef long long ...

  2. javaservlet介绍

    servlet 是 serve applet的意思  Java servlet是用Java编写的服务器端程序.其主要功能在于交互式地浏览和修改数据,生成动态Web内容. Servlet运行于支持Jav ...

  3. 424 Longest Repeating Character Replacement 替换后的最长重复字符

    给你一个仅由大写英文字母组成的字符串,你可以将任意位置上的字符替换成另外的字符,总共可最多替换 k 次.在执行上述操作后,找到包含重复字母的最长子串的长度.注意:字符串长度 和 k 不会超过 104. ...

  4. hdu3436Queue-jumpers(线段树)

    链接 这题最不好求的一部分是rank部分 因为top每次都是把一个数放在队头 不会穿插在数组里 也就是说后面没有top过的一部分数 依旧保持有序 这样可以分为两部分来处理 比如 1 2 3 4 5 6 ...

  5. 使用Dotfuscator保护.NET DLL加密DLL,防止DLL反编译

    1.下载地址 https://pan.baidu.com/s/1ztWlBxw1Qf462AE7hQJQRg 2.操作步骤 2.1安装后打开DotfuscatorPro软件,如下图所示: 2.2 选择 ...

  6. js 宿主对象的属性和方法总结

    (1)属性:       //height,width;           a=document.documentElement.clientHeight;           //文档可视高度,由 ...

  7. hihocoder编程练习赛52-3 部门聚会

    思路: 树形dp. 实现: #include <bits/stdc++.h> using namespace std; ; int n, a[MAXN], in[MAXN]; vector ...

  8. HttpWebRequest 以及WebRequest的使用

    1.WebRequest的发送数据以及接收数据 class Program { static void Main(string[] args) { //创建一个实例并发送请求 HttpWebReque ...

  9. 请大家帮我找一找bug —— 一个MySQL解析程序(JAVA实现)

    周末两天我写了一个MySQLParser.写这个东西的目的是:公司的一个项目中需要对数据打版本号(每个表的每条记录要有一个版本号字段,这个字段需要由框架自动打上去,而不是由程序员来做). 所以,我写的 ...

  10. Java对Redis基本使用

    1 引入jar包 java是通过Jedis对redis进行操作的,首先引入jedis.jar <dependency> <groupId>redis.clients</g ...