xtu summer individual-4 D - Martian Strings
Martian Strings
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 a, b, c, d, (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
- ABCBABA
2
BAAB
ABBA
- 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
- #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的更多相关文章
- Codeforces 149 E. Martian Strings
正反两遍扩展KMP,维护公共长度为L时.出如今最左边和最右边的位置. . .. 然后枚举推断... E. Martian Strings time limit per test 2 seconds m ...
- CodeForces 149E Martian Strings exkmp
Martian Strings 题解: 对于询问串, 我们可以从前往后先跑一遍exkmp. 然后在倒过来,从后往前跑一遍exkmp. 我们就可以记录下 对于每个正向匹配来说,最左边的点在哪里. 对于每 ...
- xtu summer individual 2 C - Hometask
Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...
- Codeforces149E - Martian Strings(KMP)
题目大意 给定一个字符串T,接下来有n个字符串,对于每个字符串S,判断是否存在T[a-b]+T[c-d]=S(1 ≤ a ≤ b < c ≤ d ≤ length(T)) 题解 对于每个字符串S ...
- codeforces 149E . Martian Strings kmp
题目链接 给一个字符串s, n个字符串str. 令tmp为s中不重叠的两个连续子串合起来的结果, 顺序不能改变.问tmp能形成n个字符串中的几个. 初始将一个数组dp赋值为-1. 对str做kmp, ...
- xtu summer individual 4 C - Dancing Lessons
Dancing Lessons Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- xtu summer individual 3 C.Infinite Maze
B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- xtu summer individual 2 E - Double Profiles
Double Profiles Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...
- 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 ...
随机推荐
- Cunning Gena CodeForces - 417D
Cunning Gena CodeForces - 417D 题意 先将小伙伴按需要的监视器数量排序.然后ans[i][j]表示前i个小伙伴完成j集合内题目所需最少钱.那么按顺序枚举小伙伴,用ans[ ...
- jmeter(十六)Jmeter之Bean shell使用(一)
一.什么是Bean Shell BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法; BeanShell是一种松散类型的脚本语言(这点和JS类似); BeanS ...
- 208 Implement Trie (Prefix Tree) 字典树(前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个方法.注意:你可以假设所有的输入都是小写字母 a-z.详见:https://leetcode.co ...
- 转】MongoDB 自动分片 auto sharding
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/4/ 感谢! MongoDB 自动分片 auto shard ...
- 转】R利剑NoSQL系列文章 之 Cassandra
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/3/ 感谢! R利剑NoSQL系列文章 之 Cassandr ...
- 函数的返回值return
'''1.什么是返回值 返回值是一个函数的处理结果 2.为什么要有返回值 如果我们需要在程序中拿到函数的处理结果做进一步的处理,则需要函数必须有返回值 3.函数的返回值的应用 函数的返回值用retur ...
- hihocoder编程练习赛52-2 亮灯方案
思路: 状态压缩dp.实现: #include <bits/stdc++.h> using namespace std; typedef long long ll; ; ] = {, , ...
- 解决webstromm标签高亮问题
2017/2016版
- mysql 插入多条记录,重复值不插入
只去除主键与唯一索引的字段,字段为null时 是可以重复插入的domo: insert ignore into table_name(email,phone,user_id) values('test ...
- iOS微信页面 长按图片出现【存储图像】和【拷贝】不出现【发送朋友】【保存图片】
最近遇到一大坑.微信加载的页面中出现图片,长按图片时不出现默认的菜单[发送朋友]等而是[存储图像]和拷贝. 原因:正常在页面中长按图片是没有问题的,但是如果你的页面嵌入了ifram然后又长按在ifra ...