上周比赛的题目,由于那个B题被神编译器的优化功能给卡了,就没动过这个题,其实就是个字典树嘛。当然,由于要在Boggle矩阵里得到初始序列,我还一度有点虚,不知道是用BFS还是DFS,最后发现DFS要好一些,但是会不会超时呢,我就先敲了DFS部分,先在DFS里面输出所有情况,发现总共搜完只有24W+的情况,然后字典树的匹配几乎是常数时间(因为字符串最大长度只有8)。。。所以就试着做了一下,WA了几次,发现是数组开小了,好久没做字典树的题,我只开了节点个数目的数组,这肯定不对啊,最大可能是30W(字符串总数) *8(同上),当然实际也不会打得这么离谱,因为总共才26个字母,重合的几率很大,所以最多开个30W*2其实就够了。

今天敲字典树之后突然有种想法,发现以前敲这个都是对着模板敲,现在感觉对这个算法已经很理解了,所以我随便自己怎么发挥,没用结构体,没用指针,照样很好的实现了。

像AC自动机我还是不是特别熟练,但是我觉得再照模板敲肯定学得更慢,学算法,真的不要学形,而是要学神。

这个题目也有用AC自动机处理的,确实可以优化一些,失败的话,对应DFS回退,然后树上是对应失败节点,不过处理起来要复杂一些。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char mat[][];
char wlist[][];
int vis[];
int w,n,inq[][];
char rec[];
int dir[][]={{,},{,},{,-},{-,},{,},{,-},{-,-},{-,}};
int cnt,ch[][],val[],ids[];
int point[]={,,,,,,,,,,};
int ans_num,ans_point;
char anschar[];
void inserts(char* s,int num)
{
int rt=;
int len=strlen(s);
for (int i=;i<len;i++){
int k=s[i]-'A';
if (ch[rt][k]==-){
ch[rt][k]=cnt++;
}
rt=ch[rt][k];
}
val[rt]=point[len];
ids[rt]=num;
}
void solve(char* s)
{
int rt=;
int len=strlen(s);
for (int i=;i<len;i++){
int k=s[i]-'A';
if (ch[rt][k]==-) break;
rt=ch[rt][k];
}
if (val[rt]!=- && vis[ids[rt]]==){
vis[ids[rt]]=;
ans_num++;
ans_point+=val[rt];
if (ans_num==)
memcpy(anschar,s,);
else if (len==strlen(anschar) && strcmp(s,anschar)<)
memcpy(anschar,s,);
else if (len>strlen(anschar))
memcpy(anschar,s,);
}
}
void dfs(int x,int y,int d)
{
if (d>) return;
rec[d-]=mat[x][y];
rec[d]='\0';
solve(rec);
inq[x][y]=;
int tmp;
for (int i=;i<;i++){
int nx=x+dir[i][];
int ny=y+dir[i][];
if (nx< || ny< || nx>= || ny>=) continue;
if (inq[nx][ny]) continue;
inq[nx][ny]=;
dfs(nx,ny,d+);
inq[nx][ny]=;
}
inq[x][y]=;
}
int main()
{
//freopen("rand.in","r",stdin);
//freopen("rand.out","w",stdout);
while (scanf("%d",&w)!=EOF)
{ memset(ch,-,sizeof ch);
memset(val,-,sizeof val);
memset(ids,-,sizeof ids);
cnt=;
for (int i=;i<w;i++){
scanf("%s",wlist[i]);
inserts(wlist[i],i);
}
scanf("%d",&n);
for (int i=;i<n;i++){
memset(vis,,sizeof vis);
ans_num=ans_point=;
for (int j=;j<;j++) {scanf("%s",mat[j]);}
for (int q=;q<;q++)
for (int k=;k<;k++){
dfs(q,k,);
}
printf("%d %s %d\n",ans_point,anschar,ans_num);
} }
return ;
}

GCPC 2013_A Boggle DFS+字典树 CSU 1457的更多相关文章

  1. POJ 3764 - The xor-longest Path - [DFS+字典树变形]

    题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-w ...

  2. hdu 1979 DFS + 字典树剪枝

    http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...

  3. POJ 1816 - Wild Words - [字典树+DFS]

    题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...

  4. HDU 1298 T9(字典树+dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...

  5. HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序

    题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...

  6. HDU1298 字典树+dfs

    T9 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

  7. ACM学习历程—CSU 1216 异或最大值(xor && 贪心 && 字典树)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1216 题目大意是给了n个数,然后取出两个数,使得xor值最大. 首先暴力枚举是C(n,  ...

  8. AcWing:144. 最长异或值路径(dfs + 01字典树)

    给定一个树,树上的边都具有权值. 树中一条路径的异或长度被定义为路径上所有边的权值的异或和: ⊕ 为异或符号. 给定上述的具有n个节点的树,你能找到异或长度最大的路径吗? 输入格式 第一行包含整数n, ...

  9. Codeforces 979 字符串强制N变换最多出现字母 DFS子树 暴力01字典树

    A /* Huyyt */ #include <bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define mkp(a,b) ...

随机推荐

  1. ttf格式文件

    TTF(TrueTypeFont):是一种字库名称.TTF文件:是Apple公司和Microsoft公司共同推出的字体文件格式.要使用的下载的字体文件只要把它(*.ttf)放到C:\WINDOWS\F ...

  2. python-python基础6(面向对象)

    一.面向对象编程 编程范式 编程是 程序 员 用特定的语法+数据结构+算法组成的代码来告诉计算机如何执行任务的过程 , 一个程序是程序员为了得到一个任务结果而编写的一组指令的集合,正所谓条条大路通罗马 ...

  3. NO27 定时任务

    linux定时任务的设置   为当前用户创建cron服务 1.  键入 crontab  -e 编辑crontab服务文件 例如 文件内容如下: */2 * * * * /bin/sh /home/a ...

  4. iplimage 转HBITMAP

    HBITMAP IplImage2hBitmap(IplImage* pImg) { cvFlip(pImg, NULL); BYTE tmp[]; BITMAPINFO *bmi = (BITMAP ...

  5. cmf公共函数解析

    cmf公共函数解析-common.php 路径:thinkcmf\simplewind\cmf\common.php方法: 方法 作用 返回值 cmf_get_current_admin_id    ...

  6. leetcode1019 Next Greater Node In Linked List

    """ We are given a linked list with head as the first node. Let's number the nodes in ...

  7. window安装dlib、face_recognition

    face_recognition简介 face_recognition是Python的一个开源人脸识别库,支持Python 3.3+和Python 2.7.引用官网介绍: Recognize and ...

  8. MyBatis 逆向工程(MyBatis 自动生成接口以及xml)的使用

    刚学MyBatis逆向工程(还以为要反汇编呢.....) MyBatis逆向工程 个人理解就是链接数据库自动生成相关的增删改查相关的类 以及xml文件 (其中有一些不足 应该就是多表链接的问题需要自己 ...

  9. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):带有字体图标的导航栏

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. netty权威指南学习笔记一——NIO入门(1)BIO

    公司的一些项目采用了netty框架,为了加速适应公司开发,本博主认真学习netty框架,前一段时间主要看了看书,发现编程这东西,不上手还是觉得差点什么,于是为了加深理解,深入学习,本博主还是决定多动手 ...