cf727e
题意:给你一个模式串和一堆长度相同的不相同的匹配串,问是否有一个方案可以让这个模式串由这些匹配串首尾相连组成,每个串只能出现一次.
思路:还是比较简单的,显然模式串每个位置最多匹配一个匹配串,因为所有的匹配串严格不同,每个位置有没有匹配哪个匹配串用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的更多相关文章
- 【CodeForces727E/CF727E】Games on a CD (字符串哈希)
题目: CodeForces727E 分析: 看到字符串比较,肯定想到哈希啊--现学的哈希,先丢两个重要的公式 (\(seed\)是大于字符集大小的质数,\(p\)是大质数) \[hash[i]=(h ...
随机推荐
- things to analysis
Started by timer [EnvInject] - Loading node environment variables. Building remotely on RE_DEV_BUILD ...
- java @ResponseBody返回值中去掉NULL字段
需要同时添加两个位置: 1.annotation-driven过滤 <mvc:annotation-driven> <mvc:message-converters register- ...
- ArcGIS Engine渲染
符号化之Renderer( 渲染)体系 ArcGIS Engine9.3对GIS数据的符号化分为矢量数据渲染和栅格数据渲染两大类.接下来分别介绍FeatureRender和RasterRender. ...
- android 自定义View Caused by: java.lang.ClassNotFoundException: Didn't find class
在android studio中, 自定义View 时,出现 Caused by: java.lang.ClassNotFoundException: Didn't find class 在查看包名和 ...
- html基础 链接
使用图像来作链接: (链接标签中嵌套图片标签) <html> <body> <p> 您也可以使用图像来作链接: <a href="/example/ ...
- ebs如何将客户化的PL/SQL程序发布到webservice
as:cux_gl_hec_iface_soa_pkg. 1.将package声明部分的内容拷贝出来另存为cux_gl_hec_iface_soa_pkg.pls的文件: 2.将该文件上传到服务器上拥 ...
- 在iframe中使用cookie需要注意
cookie的使用早已不新鲜了,但是最近在做项目时还是被坑了一把. 那么接下来让我们来看一下这个"坑"是个什么情况! 前提: 1.现在有两个页面A.html, B.html,同时, ...
- js复制
JS实现各种复制到剪贴板 一.实现点击按钮,复制文本框中的的内容 <script type="text/javascript" ...
- JS面向对象的程序设计
面向对象的语言有一个标志,即拥有类的概念,抽象实例对象的公共属性与方法,基于类可以创建任意多个实例对象,一般具有封装.继承.多态的特性!但JS中对象与纯面向对象语言中的对象是不同的,ECMA标准定义J ...
- Webservice接口
快递查询接口 http://webservice.36wu.com/ExpressService.asmxip查询接口 http://webservice.36wu.com/ipService.asm ...