http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1620

ProblemG

ILove Strings!!!

Input:Standard Input

Output:Standard Output

TimeLimit: 4 Seconds

Hmmmmmm…………strings again :) Then it must be an easy task for you. You are given with a string S of length less than 100,001, containing only characters from lower and uppercase English alphabet (‘a’-‘z’and ‘A’ – ‘Z’).
Then follows q (q < 100) queries where each query contains a string T of maximum length 1,000(also contains only ‘a’-‘z’ and ‘A’ – ‘Z’). You have to determine whether or not T is a sub-string of S.

Input

First line contains aninteger k (k < 10) telling the number of test cases to follow.Each test case begins with S. It is followed by q.After this line there are q lines each of which has a string T as defined before.

Output

For each query print ‘y’if it is a sub-string of S or ‘n’ otherwise followed by a newline. See the sample output below.

Sample Input

2

abcdefghABCDEFGH

2

abc

abAB

xyz

1

xyz

Output for Sample Input

y

n

y

Problemsetter: MohammadSajjad Hossain

题意:

给出一个文本串和若干个模式串,问模式串是否在文本串中出现过。

分析:

简单粗暴的AC自己主动机模板题。要注意模式串可能有反复的情况。

/*
*
* Author : fcbruce
*
* Time : Sat 04 Oct 2014 03:30:15 PM CST
*
*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10 #ifdef _WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif #define maxm
#define maxn 1000007
#define maxsize 52 using namespace std; int q[maxn<<1];
int _id[1007];
char YN[1007];
char query[1007];
char T[maxn]; struct Trie
{
int ch[maxn][maxsize];
int val[maxn];
int sz;
Trie()
{
sz=1;
val[0]=0;
memset(ch[0],0,sizeof ch[0]);
}
void clear()
{
sz=1;
val[0]=0;
memset(ch[0],0,sizeof ch[0]);
}
int idx(const char ch)
{
if (islower(ch)) return ch-'a'+26;
return ch-'A';
} int insert(const char *s,int v=1)
{
int u=0,l=strlen(s);
for (int i=0;i<l;i++)
{
int c=idx(s[i]);
if (ch[u][c]==0)
{
val[sz]=0;
memset(ch[sz],0,sizeof ch[0]);
ch[u][c]=sz++;
}
u=ch[u][c];
} if (val[u]!=0) return val[u];
return val[u]=v;
}
}; struct ACauto :public Trie
{
int cnt;
int last[maxn];
int nex[maxn]; ACauto()
{
cnt=0;
sz=1;
val[0]=0;
memset(ch[0],0,sizeof ch[0]);
}
void clear()
{
cnt=0;
sz=1;
val[0]=0;
memset(ch[0],0,sizeof ch[0]);
} void calc(int j)
{
if (j!=0)
{
YN[val[j]]='y';
calc(last[j]);
}
} void get_fail()
{
int f=0,r=-1;
nex[0]=0;
for (int c=0;c<maxsize;c++)
{
int u=ch[0][c];
if (u!=0)
{
nex[u]=0;
q[++r]=u;
last[u]=0;
}
} while (f<=r)
{
int x=q[f++];
for (int c=0;c<maxsize;c++)
{
int u=ch[x][c];
if (u==0) continue;
q[++r]=u;
int v=nex[x];
while (v>0 && ch[v][c]==0) v=nex[v];
nex[u]=ch[v][c];
last[u]=val[nex[u]]>0? nex[u]:last[nex[u]];
}
}
} void find(const char *T)
{
get_fail();
for (int i=0,j=0;T[i]!='\0';i++)
{
int c=idx(T[i]);
while (j>0 && ch[j][c]==0) j=nex[j];
j=ch[j][c];
if (val[j]!=0)
calc(j);
else if (last[j]!=0)
calc(last[j]);
}
}
}acauto; int main()
{
#ifdef FCBRUCE
freopen("/home/fcbruce/code/t","r",stdin);
#endif // FCBRUCE int T_T;
scanf ("%d",&T_T); while (T_T--)
{
acauto.clear();
scanf("%s",T); int n;
scanf("%d",&n); for (int i=1;i<=n;i++)
{
scanf("%s",query);
_id[i]=acauto.insert(query,i);
YN[i]='n';
} acauto.find(T); for (int i=1;i<=n;i++)
printf("%c\n",YN[_id[i]]);
} return 0;
}

UVA 10679 I love Strings!!!(AC自己主动机)的更多相关文章

  1. 【UVA】1449-Dominating Patterns(AC自己主动机)

    AC自己主动机的模板题.须要注意的是,对于每一个字符串,须要利用map将它映射到一个结点上,这样才干按顺序输出结果. 14360841 1449 option=com_onlinejudge& ...

  2. uva 11468 - Substring(AC自己主动机+概率)

    题目链接:uva 11468 - Substring 题目大意:给出一些字符和各自字符相应的选择概率.随机选择L次后得到一个长度为L的字符串,要求该字符串不包括随意一个子串的概率. 解题思路:构造AC ...

  3. 【UVA】11468-Substring(AC自己主动机)

    AC自己主动机的题,须要注意的,建立失配边的时候,假设结点1失配边连到的那个结点2,那个结点2是一个单词的结尾,那么这个结点1也须要标记成1(由于能够看成,这个结点包括了这个单词),之后在Trie树上 ...

  4. POJ 3691 &amp; HDU 2457 DNA repair (AC自己主动机,DP)

    http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...

  5. hdu4758 Walk Through Squares (AC自己主动机+DP)

    Walk Through Squares Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...

  6. poj 3691 DNA repair(AC自己主动机+dp)

    DNA repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5877   Accepted: 2760 Descri ...

  7. HDOJ 5384 Danganronpa AC自己主动机

     AC自己主动机裸题 Danganronpa Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java ...

  8. HDU - 4758 Walk Through Squares (AC自己主动机+DP)

    Description   On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...

  9. hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数

    http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franc ...

随机推荐

  1. ios手机弹出层上表单的操作,收起键盘焦点错乱的问题

    今天遇到了ios手机下 弹出层上form表单 当收起键盘后,焦点错乱,无法再操作的问题 解决办法 function device() { const u = navigator.userAgent; ...

  2. 【IDEA】IDEA设置修改完JS和JSP不用重启的办法(IDEA热部署)

    修改完JS和JSP不停的重启服务器真的很烦,所以设置修改完之后不用重启也生效: 前提有两个: 确保使用的是debug模式. 确保tomcat是由idea实例化的.也就是说tomcat是在idea中配置 ...

  3. JS多个函数之间传递参数问题

    JS多个函数之间传递参数的一个重要思想是在页面定义一个隐藏域,当第一个函数请求到数据时候修改隐藏域的值,第二个函数用jQuery的选择器选择页面中隐藏域的值. 比如: 页面中定义一个隐藏的页号. &l ...

  4. Access数据库 INSERT INTO 失败

    一次操作Access数据库,插入一条数据,总是失败,如下: 通过赋值,一个字段一个字段的排查,最终确定是UserAge字段处有问题. 最初,UserAge字段是 %d 类型的,赋值20,可成功插入数据 ...

  5. NS_AVAILABLE_IOS(6_0)

    http://www.cocoachina.com/bbs/read.php?tid=241951 一个简单的小问题,请诸位大侠帮助给看看 ,新手 ,勿拍砖       本帖属于CocoaChina会 ...

  6. Linux内核实践之序列文件【转】

    转自:http://blog.csdn.net/bullbat/article/details/7407194 版权声明:本文为博主原创文章,未经博主允许不得转载. 作者:bullbat seq_fi ...

  7. python的递归算法学习(1)

    递归函数在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数.举个例子,我们来计算阶乘 n! = 1 * 2 * 3 * ... * n,用函数 fact(n)表示,可以 ...

  8. hdu 5162(水题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5162 题解:看了半天以为测试用例写错了.这题玩文字游戏.它问的是当前第i名是原数组中的第几个. #i ...

  9. EntityFramework之多对多关系(四)

    上篇介绍了一对多关系,下面介绍下多对多关系代码编写. 1.新建model实体,User是用户类,Role是角色类,由于是多对多关系,必须得有一个中间类,所以产生了UserRole类 public cl ...

  10. Python_Tips[4] -> and 和 or 的计算原则

    and和or / and & or 对于and和or,可以连接多个值,其分别遵循原则: 全是 And: 返回第一个遇到的无效值,若全有效,返回最后一个有效值 全是 Or: 返回第一个遇到的有效 ...