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;
}

最新文章

  1. Android 卡片计数器
  2. Pattern Recognition And Machine Learning读书会前言
  3. Visiual Studio2012 CLR20r3问题
  4. ssh项目删除
  5. 关于SVM一篇比较全介绍的博文
  6. Sublime Text 2 新建C++ build system
  7. mysqldump 备份直接至压缩文件,提高备份及压缩时间
  8. CLR Via C#: 类型基础
  9. shared_lock and unique_lock
  10. 故障公告:IIS应用程序池停止工作造成博客站点无法访问
  11. MongoDB 创建索引的语法
  12. D - Mayor&#39;s posters(线段树+离散化)
  13. [原创]K8 DB_Owner权限GetShell工具
  14. pm2启动jenkins不存在tty的问题
  15. Web应用:当文件超过100KB,无法上传,有种原因你想象不到
  16. .Net MVC 当前上下文中不存在名称“Style”
  17. java编写的Http协议的多线程下载器
  18. 第三节 java 函数的封装方法 以及 访问封装内容
  19. 怎么归档老日志的shell脚本
  20. 【黑金ZYNQ7000系列原创视频教程】04.熟悉ZYNQ内部中断——内部定时器中断实验

热门文章

  1. mysql 连接失败问题汇集
  2. android上USB Wifi调试记录
  3. HDU5469 Antonidas(树分治&amp;&amp;哈希)
  4. HDU 5886 Tower Defence(2016青岛网络赛 I题,树的直径 + DP)
  5. Python的程序结构[1] -&gt; 方法/Method[3] -&gt; 魔术方法 __getattr__ 与代理模式
  6. Python的程序结构[2] -&gt; 类/Class[0] -&gt; 类的特殊属性
  7. 洛谷—— P3908 异或之和
  8. UTF-8 setup for workspace
  9. 又是一年NOIP然鹅我考的是高数(虽然我没打并且内容与NOIP无关)(手动滑稽)
  10. 2.2多线程(java学习笔记)线程状态及线程操作的相关方法