题意:给你一个模式串和一堆长度相同的不相同的匹配串,问是否有一个方案可以让这个模式串由这些匹配串首尾相连组成,每个串只能出现一次.

思路:还是比较简单的,显然模式串每个位置最多匹配一个匹配串,因为所有的匹配串严格不同,每个位置有没有匹配哪个匹配串用ac自动机很容易就能跑出来,然后枚举一下位置就ok,还有模式串应该在开头加上尾部的一些字符,因为要求的是这个模式串要看成一个环.

 #include <bits/stdc++.h>
using namespace std;
char aim[];
int pos[];
char temp[];
int total;
struct Root
{
Root *next[];
int count;
Root *fail;
Root()
{
for(int i=; i<; i++)
next[i]=NULL;
fail=NULL;
count=;
}
};
queue<Root *>q;
class AC_auto
{
public:
AC_auto();
void insert(char object[],int num);
void ac_auto();
void match(char aim[]);
private:
Root *root;
};
AC_auto::AC_auto()
{
root=new Root();
}
void AC_auto::insert(char object[],int num)
{
int len=strlen(object);
Root *temp=root;
for(int i=; i<len; i++)
{
if(temp->next[object[i]-'a']==NULL)
{
Root *temp1=new Root();
temp->next[object[i]-'a']=temp1;
}
temp=temp->next[object[i]-'a'];
}
temp->count=num;
}
void AC_auto::ac_auto()
{
while(!q.empty())q.pop();
q.push(root);
Root *temp1,*temp2;
while(!q.empty())
{
temp1=q.front();
q.pop();
for(int i=; i<; i++)
{
if(temp1->next[i])
{
if(temp1==root)
temp1->next[i]->fail=root;
else
{
temp2=temp1->fail;
while(temp2)
{
if(temp2->next[i])
{
temp1->next[i]->fail=temp2->next[i];
break;
}
temp2=temp2->fail;
}
if(temp2==NULL)
temp1->next[i]->fail=root;
}
q.push(temp1->next[i]);
}
}
}
}
void AC_auto::match(char aims[])
{
int len=strlen(aims);
Root *temp1,*temp=root;
for(int i=; i<len; i++)
{
if(aims[i]>'z'||aims[i]<'a')
{
temp=root;
continue;
}
while(temp->next[aims[i]-'a']==NULL&&temp!=root)
temp=temp->fail;
temp=temp->next[aims[i]-'a'];
if(!temp)temp=root;
if(temp->count)
pos[i]=temp->count;
}
}
bool sign[];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
scanf("%s",aim+k);
for(int i=;i<k;i++)
aim[i]=aim[(n+)*k-k+i];
AC_auto *ac_auto=new AC_auto();
int g;
scanf("%d",&g);
for(int i=;i<=g;i++)
{
scanf("%s",temp);
ac_auto->insert(temp,i);
}
ac_auto->ac_auto();
ac_auto->match(aim);
for(int i=k;i<*k;i++)
{
for(int j=i;j<n*k+k;j+=k)
{
if(pos[j]==)
break;
else if(sign[pos[j]])
break;
sign[pos[j]]=true;
if(j+k>=n*k+k)
{
printf("YES\n");
for(int j=i;j<n*k+k;j+=k)
printf("%d ",pos[j]);
return ;
}
}
for(int j=i;j<n*k+k;j+=k)
{
if(pos[j]==)
break;
sign[pos[j]]=false;
}
}
printf("NO\n");
return ;
}

cf727e的更多相关文章

  1. 【CodeForces727E/CF727E】Games on a CD (字符串哈希)

    题目: CodeForces727E 分析: 看到字符串比较,肯定想到哈希啊--现学的哈希,先丢两个重要的公式 (\(seed\)是大于字符集大小的质数,\(p\)是大质数) \[hash[i]=(h ...

随机推荐

  1. centos 安装rmagick 2.13.4出错

    因为安装redmine,缺少rmagick,使用bundle install安装依赖的gem,报错如下: 然后,网上查询一下,都是ubuntu系统下的解决方案. centos下正确的解决方法如下: y ...

  2. 常用SQL语句

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...

  3. 每天一个 Linux 命令(14):head 命令

    head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...

  4. C#动态编译代码,执行一个代码片段,或者从指定文件中加载某个接口的实现类

    在项目进行中有时候会需要配置一些复杂的表达式,在程序运行的时候执行表达式,根据结果执行相应的操作,简单写了一个类Expression,利用.net的动态编译技术实现,代码如下: public clas ...

  5. ubuntu14.04计划任务无法执行

    在/etc/crontab中添加了任务1,并确认执行时间设置没有错.发现任务没有执行,而/var/log/cron.log日志文件中没有该计划任务的执行信息.另一个计划任务却能正确,通过修改任务1的执 ...

  6. 提交到github远程仓库遇到的问题

    1.could not read from remote repository 可能原因是没有将ssh 密匙添加到github,所以没有权限 解决办法: 1. ssh-keygen -C 'your@ ...

  7. Silverlight 页面传值问题(转)

    共有两种方式来传递初始化参数 1)在html或者aspx页面中object对象中加入一下代码 参数格式:参数名 = 值,参数名 = 值,... <param name="initPar ...

  8. STL之序列容器vector

    首先来看看vector的模板声明: template <class T, class Alloc = allocator<T>> class vector { //… }; v ...

  9. mongoDB学习笔记:了解与安装

    初次使用mongoDB是在2013年,项目组为了新产品的研发,使用了mongoDB,作为项目组的一名测试员,也就跟着学起来了. 1.了解mongoDB Mongo DB ,是目前在IT行业非常流行的一 ...

  10. Sql Server 按行处理表数据思路

    SqlServer的游标当超过1000行左右的时候效率极其低下. DECLARE @Company VARCHAR(12)='' SELECT TOP 1 @Company=CompanyID FRO ...