题意:给出多个加密的模式串,和多个待匹配的串,问每个串里出现了多少种模式串。加密方法是把每3bytes加密成按6bits一个对应成4个字符,对应方法题里给了。

分析:除了解密之外,基本是个赤裸裸的AC自动机。这题要注意有多个模式串要进自动机,所以自动机的vis数组要每次清零。

#include <cstdio>
#include <queue>
#include <cstring>
#include <cctype>
using namespace std; #define D(x) const int MAX_CHILD_NUM = ;
const int MAX_NODE_NUM = * + ;
const int MAX_LEN = + ; char st[MAX_LEN];
int st2[MAX_LEN];
bool vis[MAX_NODE_NUM]; struct Trie
{
int next[MAX_NODE_NUM][MAX_CHILD_NUM];
int fail[MAX_NODE_NUM];
int count[MAX_NODE_NUM];
int node_cnt;
int root; void init()
{
node_cnt = ;
root = newnode();
} int newnode()
{
for (int i = ; i < MAX_CHILD_NUM; i++)
next[node_cnt][i] = -;
count[node_cnt++] = ;
return node_cnt - ;
} int get_id(int a)
{
return a;
} void insert(int buf[], int id)
{
int now = root;
for (int i = ; buf[i] != -; i++)
{
int id = get_id(buf[i]);
if (next[now][id] == -)
next[now][id] = newnode();
now = next[now][id];
}
count[now]++;
} void build()
{
queue<int>Q;
fail[root] = root;
for (int i = ; i < MAX_CHILD_NUM; i++)
if (next[root][i] == -)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
while (!Q.empty())
{
int now = Q.front();
Q.pop();
for (int i = ; i < MAX_CHILD_NUM; i++)
if (next[now][i] == -)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
} int query(int buf[])
{
int now = root;
int res = ;
for (int i = ; buf[i] != -; i++)
{
now = next[now][get_id(buf[i])];
int temp = now;
while (temp != root && !vis[temp])
{
res += count[temp];
// optimization: prevent from searching this fail chain again.
//also prevent matching again.
vis[temp] = true;
temp = fail[temp];
}
}
return res;
} void debug()
{
for(int i = ;i < node_cnt;i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],count[i]);
for(int j = ;j < MAX_CHILD_NUM;j++)
printf("%2d",next[i][j]);
printf("]\n");
}
}
}ac; int n, m; int get_value(char ch)
{
if (isupper(ch))
return ch - 'A';
if (islower(ch))
return ch - 'a' + ;
if (isdigit(ch))
return ch - '' + ;
if (ch == '+')
return ;
return ;
} void transform(char *st, int *st2)
{
int len = strlen(st);
int len2 = len * / ;
for (int i = ; i < len; i += )
{
int a = ;
for (int j = ; j < ; j++)
{
a = (a << ) + get_value(st[i + j]);
D(printf("**%d\n", a));
} for (int j = ; j >= ; j--)
{
st2[i * / + j] = a % ( << );
a >>= ;
D(printf("**%d\n", st2[i * / + j]));
}
}
while (st[len - ] == '=')
{
len--;
len2--;
}
st2[len2] = -;
D(puts("#"));
for (int i = ; i < len2; i++)
{
D(printf("%d ", st2[i]));
}
D(puts(""));
} void input()
{
for (int i = ; i <= n; i++)
{
scanf("%s", st);
transform(st, st2);
ac.insert(st2, i);
}
ac.build();
scanf("%d", &m);
for (int i = ; i < m; i++)
{
scanf("%s", st);
transform(st, st2);
memset(vis, , sizeof(vis));
printf("%d\n", ac.query(st2));
}
puts("");
} int main()
{ while (scanf("%d", &n) != EOF)
{
ac.init();
input();
}
return ;
}

zju3430的更多相关文章

随机推荐

  1. Flex入门笔记

    Test_01.mxml <?xml version="1.0" encoding="utf-8"?> <viewer:BaseWidget  ...

  2. html、css杂记

    1:浮动 <div style="float: left"> 2:清除浮动,把父div撑起来 <div style="clear:both"& ...

  3. Spring 事务传递教程_有实例

    通过这篇文章,你将学习到Spring框架中中事务的传递 简介 在处理Spring管理的事务时,开发人员可以以传播的方式定义事务的行为.换句话说,开发人员能够决定业务方法如何被封装在逻辑和物理事务中.来 ...

  4. 希望各位博友能对我的自我介绍提出意见(要面试IBM的应用开发工程师,本科应届生一枚)

    面试官你好,首先我非常高兴能参加今天的面试. 我叫XXX(我名字里面有光宗耀祖),也许父母希望我光宗耀祖吧,所以给我起这样的名字.我的家乡山西太原,本科就读于XX大学,专业是信息与计算科学. 我今天要 ...

  5. 绕过HR破门而入的求职智慧

    以往我们在网上看到的很多求职文章或指导性纲领,譬如啥自信.做功课.良好形象.华丽的简历.工作经验.口才啥的,其实到了21世纪尤其是互联网高速发展的今天,前面这些技巧就显得无比空洞: 1.因为自信谁都可 ...

  6. JLS(Third Edition) Chapter12 Execution

    这一章详细说明在一个program执行时,发生的activities. 它根据JVM和组成program的类.接口.实例的生命周期 组织.   一个JVM从加载一个特定的类并调用它的main方法开始启 ...

  7. Apache服务器httpd.exe进程占用cpu超过50%的解决方法

    httpd.exe进程占用cpu超过50%,关闭掉Apache服务,cpu应用率立刻下降到0.  重新启动Apache又出现占用cpu高的情况.  原因是:httpd.exe和防火墙配置有冲突. 解决 ...

  8. eclipse如何导入java项目文件

    平时下载到项目时,希望能够导入到eclipse中使用.但有些项目不能直接导入,需要做转换. 打开源文件目录,查询如下: 如果目录中包含pom.xml文件,则说明该项目由Maven构建的,参考以下 如何 ...

  9. 大数据之sqoopCDH 备份

    Sqoop课程笔记 一.概述 1.什么是sqoop? Hadoop的优势在于对数据的存储和处理,相比以前传统的数据库,在处理较较多的数据时,传统数据行业通过提升单机性能以提高处理性能,而且性价比随着性 ...

  10. 【PHP面向对象(OOP)编程入门教程】15.static和const关键字的使用(self::)

    static关键字是在类中描述成员属性和成员方法是静态的:静态的成员好处在哪里呢?前面我们声明了“Person”的人类,在“Person”这个类里如果我们加上一个“人所属国家”的属性,这样用“Pers ...