Games on a CD
4 seconds
512 megabytes
standard input
standard output
Several years ago Tolya had n computer games and at some point of time he decided to burn them to CD. After that he wrote down the names of the games one after another in a circle on the CD in clockwise order. The names were distinct, the length of each name was equal to k. The names didn't overlap.
Thus, there is a cyclic string of length n·k written on the CD.
Several years have passed and now Tolya can't remember which games he burned to his CD. He knows that there were g popular games that days. All of the games he burned were among these g games, and no game was burned more than once.
You have to restore any valid list of games Tolya could burn to the CD several years ago.
The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 105) — the amount of games Tolya burned to the CD, and the length of each of the names.
The second line of the input contains one string consisting of lowercase English letters — the string Tolya wrote on the CD, split in arbitrary place. The length of the string is n·k. It is guaranteed that the length is not greater than 106.
The third line of the input contains one positive integer g (n ≤ g ≤ 105) — the amount of popular games that could be written on the CD. It is guaranteed that the total length of names of all popular games is not greater than 2·106.
Each of the next g lines contains a single string — the name of some popular game. Each name consists of lowercase English letters and has length k. It is guaranteed that the names are distinct.
If there is no answer, print "NO" (without quotes).
Otherwise, print two lines. In the first line print "YES" (without quotes). In the second line, print n integers — the games which names were written on the CD. You should print games in the order they could have been written on the CD, it means, in clockwise order. You can print games starting from any position. Remember, that no game was burned to the CD more than once. If there are several possible answers, print any of them.
3 1
abc
4
b
a
c
d
YES
2 1 3
4 2
aabbccdd
4
dd
ab
bc
cd
NO
分析:字典树上的匹配,因为最多有k个容器;
把匹配好的编号分别放入相应的容器里,最后看是否满足n个即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<ll,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,sz,cur_v;
struct node
{
int nxt[],id,num,pre,link;
}trie[maxn**];
char a[maxn*],b[maxn];
set<int>p[maxn];
void insert(int x)
{
int now=;
for(int i=;i<k;i++)
{
int y=b[i]-'a';
if(!trie[now].nxt[y]){
trie[now].nxt[y]=++sz;
trie[sz].pre=now;
trie[sz].num=y;
}
now=trie[now].nxt[y];
}
trie[now].id=x;
}
int link(int v);
int go(int v, int c) {
if (trie[v].nxt[c]) {
return trie[v].nxt[c];
}
if (v == ) {
return ;
}
return trie[v].nxt[c] = go(link(v), c);
} int link(int v) {
if (trie[v].link) {
return trie[v].link;
}
if (v == || trie[v].pre == ) {
return ;
}
return trie[v].link = go(link(trie[v].pre), trie[v].num);
}
void check(int x)
{
cur_v=go(cur_v,a[x]-'a');
if(trie[cur_v].id)
{
p[(x-k+)%k].insert(cur_v);
}
}
int get_id(int x)
{
int now=;
for(int i=;i<k;i++)
{
int y=a[i+x]-'a';
now=trie[now].nxt[y];
}
return trie[now].id;
}
int main()
{
int i,j;
scanf("%d%d%s",&n,&k,a);
for(i=;i<k;i++)a[i+n*k]=a[i];
scanf("%d",&m);
rep(i,,m)
{
scanf("%s",b);
insert(i);
}
for(i=;i<n*k+k;i++)
{
check(i);
}
for(i=;i<k;i++)
{
if(p[i].size()==n)
{
puts("YES");
for(j=i;j<n*k+i;j+=k)printf("%d ",get_id(j));
return ;
}
}
puts("NO");
//system("Pause");
return ;
}
Games on a CD的更多相关文章
- CodeForces - 727E Games on a CD 字符串Hash
题意:有n个单词,每个单词长度为k,顺时针将它们写成一个圆圈串.现在知道g个长度为k的单词,是否可以从这g个单词中选择n个形成这个圆圈串?如果有多个答案,任意输出一个. 思路 可以发现,如果枚举第一个 ...
- codeforces727E. Games on a CD
题意:给一个n*k的循环字符串可能从任意地方断开,然后m个长度k的字符串,问你能不能用下面的字符串(每个最多用一次)构成上面的字符串,能循环移位 题解:对下面的串建ac自动机,记录字符串最后一个位置, ...
- 【CodeForces727E/CF727E】Games on a CD (字符串哈希)
题目: CodeForces727E 分析: 看到字符串比较,肯定想到哈希啊--现学的哈希,先丢两个重要的公式 (\(seed\)是大于字符集大小的质数,\(p\)是大质数) \[hash[i]=(h ...
- Linux系统1.md
计算机 介绍 电子计算机(英语:computer),亦称电脑,是一种利用电子学原理,根据一系列指令对数据进行处理的工具. 在现代,机械计算机的应用已经完全被电子计算机所替换,其所相关的技术研究叫计算机 ...
- 【Linux探索之旅】第二部分第三课:文件和目录,组织不会亏待你
内容简介 1.第二部分第三课:文件和目录,组织不会亏待你 2.第二部分第四课预告:文件操纵,鼓掌之中 文件和目录,组织不会亏待你 上一次课我们讲了命令行,这将成为伴随我们接下来整个Linux课程的一个 ...
- 什么是shell? bash和shell有什么关系?
什么是shell? bash和shell有什么关系? 博客分类: Linux 什么是Shell? shell是你(用户)和Linux(或者更准确的说,是你和Linux内核)之间的接口程序 ...
- bash 基础命令
bash的基础特性(): () 命令历史 history 环境变量: HISTSIZE:命令历史记录的条数: HISTFILE:~/.bash_history: HISTFILESIZE:命令历史文件 ...
- 什么是shell? bash和shell有什么关系?
什么是Shell? shell是你(用户)和Linux(或者更准确的说,是你和Linux内核)之间的接口程序.你在提示符下输入的每个命令都由shell先解释然后传给Linux内核. ...
- 【Linux探索之旅】第二部分第三课:文件和文件夹,组织不会亏待你
wx_fmt=jpeg" alt="" style="max-width:100%; height:auto!important"> 内容简单介 ...
随机推荐
- 遍历(一)jquery $().each和$.each()
原文:http://www.frontopen.com/1394.html 在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法. $().each 在dom处理上 ...
- ggplot2 theme相关设置—线条设置
在ggplot的主题射中有一部分图需要对图中的部分线条进行设置 element_line(colour = NULL, size = NULL, linetype = NULL, lineend = ...
- 浙大pat 1031题解
1031. Hello World for U (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Giv ...
- 使用紧凑的序列化器,数倍提升性能 —— ESFramework 4.0 快速上手(11)
在分布式通信系统中,网络传递的是二进制流,而内存中是我们基于对象模型构建的各种各样的对象,当我们需要将一个对象通过网络传递给另一个节点时,首先需要将其序列化为字节流,然后通过网络发送给目标节点,目标节 ...
- java方法的多态性理解
1.什么是java的多态 浏览了别人博客中的一些介绍多态的文章,发现大家的描述有点不一样,主要区别在于是否把方法的重写算做多态.一种我比较认同的说法如下: 多态分为两种 a. 编译时多态:方法的重载: ...
- def语句常见错误
自觉不才,使用def语句时容易出现以下错误, 参考: 习题—25 http://www.2cto.com/shouce/Pythonbbf/ex25.html def add(): print &qu ...
- 2015 Multi-University Training Contest 4
1001 Olympiad 签到题1. # include <iostream> # include <cstdio> using namespace std; ]={}; b ...
- 1-10w之间的整数中有几个完全平方数
#include "stdio.h" #include<math.h> void main() { ,x,y; printf("1-10w之间的整数中有以下几 ...
- 转载 deep learning:八(SparseCoding稀疏编码)
转载 http://blog.sina.com.cn/s/blog_4a1853330102v0mr.html Sparse coding: 本节将简单介绍下sparse coding(稀疏编码),因 ...
- sql 生成开始日期到结束日期与开始日期的相差天数
SELECT CONVERT(VARCHAR(10), DATEADD(dd, number, '2015-10-25'), 120) AS dtDate FROM ...