题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1457

读了一遍题想到了字典树,但不知道怎么写,队友一个单词一个单词的枚举,然后暴搜过了,赛后查题解发现就是字典树+暴搜,比队友写的少了几百毫秒

#include <bits/stdc++.h>
using namespace std;
int trie_n;
const int MAXN = 400006;
char mat[5][5];
bool vis[5][5],book[MAXN];
int to[8][2] = {{0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,-1},{-1,1}};
int point[] = {0,0,0,1,1,2,3,5,11,11,11};
struct Trie_node
{int next[26];int index,val;Trie_node(){index = val = -1;fill(next,next + 26,-1);}
};
char temp[10];
Trie_node trie[MAXN];
int ans1,ans2;
char word[15];
void trie_insert(char s[],int index)
{int root = 0;int len = strlen(s);for(int i = 0; i < len; i++) {int next = trie[root].next[s[i] - 'A'];if(next == -1) {next = trie[root].next[s[i] - 'A'] = trie_n++;}root = next;}trie[root].index = index;trie[root].val = point[len];
}
void solve(char *s)
{int root = 0;int len = strlen(s);for(int i = 0; i < len; i++) {int next = trie[root].next[s[i] - 'A'];if(next == -1) break;root = next;}if(trie[root].val != -1 && !book[trie[root].index]) {book[trie[root].index] = 1;ans1++;ans2 += trie[root].val;if(ans1 == 1) memcpy(word,s,10);else if(len == strlen(word) && strcmp(s,word) < 0)memcpy(word,s,10);else if(len > strlen(word))memcpy(word,s,10);}
}
void dfs(int x,int y,int cur)
{if(cur > 8) return;temp[cur - 1] = mat[x][y];temp[cur] = '\0';solve(temp);vis[x][y] = 1;for(int i = 0; i < 8; i++) {int tx = x + to[i][0];int ty = y + to[i][1];if(tx < 0 || ty < 0 || tx >= 4 || ty >= 4) continue;if(vis[tx][ty]) continue;vis[tx][ty] = 1;dfs(tx,ty,cur + 1);vis[tx][ty] = 0;}vis[x][y] = 0;
}
int main(void)
{int n;char str[20];scanf("%d",&n);trie_n = 1;for(int i = 1; i <= n; i++) {scanf("%s",str);trie_insert(str,i);}int q;scanf("%d",&q);while(q--) {ans1 = ans2 = 0;memset(book,false,sizeof(book));for(int i = 0; i < 4; i++) scanf("%s",mat[i]);for(int i = 0; i < 4; i++) {for(int j = 0; j < 4; j++) {dfs(i,j,1);}}printf("%d %s %d\n",ans2,word,ans1);}return 0;
}

CSU 1457 Boggle (字典树+DFS)相关推荐

  1. Trie树 + DFS - CSU 1457 Boggle

    Boggle Problem's Link:  http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1457 Mean: 给定n个串,有m个询问. 每个询 ...

  2. hust1350Trie【字典树+dfs || 字典树 + LCA】

    大意:告诉你一些字符串 让你组成字典树, 然后定义每个节点到所有叶子节点的距离的和等于改点的value 当根节点只有一个孩子,该根节点也算一个叶子节点 问所有节点的value的最小值 分析: 开始做的 ...

  3. hdu 1298 字典树 + DFS (模拟T9文本输入)

    题意:       给你一些按键顺序,让你输出每一步中概率最大的那个单词,这里的概率计算方 法好好看看别弄错了,一开始就是因为弄错了,各种wa,比如 abc 1 ,ab 1,那么 ab 的概率就是2 ...

  4. 2019 ICPC 南京 F. Paper Grading(字典树dfs序上树套树)

    Paper Grading 题意:给定nnn个字符串,有两种操作: 一.给定i,ji, ji,j,交换第iii个跟第jjj个字符串. 二.给定 str ,k,l,rk, l, rk,l,r,问你在区间 ...

  5. POJ 3764 Language: The xor-longest Path (01字典树+DFS)

    传送门:POJ 3764 题目大意: 在树上找一段路径(连续)使得边权相异或的结果最大. 前置技能: 1.用链式前向星建图. 2. 01字典树的应用. 思路: 本题用 vector数组建图是会超时的, ...

  6. [Scoi2016]背单词[字典树+dfs重构树[类似虚树]]

    解题思路:很明显第一个条件是可以避免的,第二个条件是第三个条件的特殊情况,所以有用的只有第三个条件,现在我们就是想将这些单词重排使得每个单词后缀都在这个单词的前面并且代价最小 我们举个例子: 6 a ...

  7. GCPC 2013_A Boggle DFS+字典树 CSU 1457

    上周比赛的题目,由于那个B题被神编译器的优化功能给卡了,就没动过这个题,其实就是个字典树嘛.当然,由于要在Boggle矩阵里得到初始序列,我还一度有点虚,不知道是用BFS还是DFS,最后发现DFS要好 ...

  8. CSU 1115: 最短的名字(字典树)

    1115: 最短的名字 题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1115 Description 在一个奇怪的村子中,很多人的名字都 ...

  9. 【CODE】Unique Paths Word Search (DFS dp 字典树)

    目录 62. Unique Paths 63. Unique Paths II 980. Unique Paths III 79. Word Search 212. Word Search II 字典 ...

最新文章

  1. 2、程序包 Packages
  2. 【单机实现系列】SCDPM2012实现数据保护
  3. 配置hosts快速访问GitHub
  4. 10个免费的PHP编辑器/开发工具
  5. classmethod作用
  6. 工业机器人技术试题_工业机器人考试试题库
  7. DEM高程数据获取方法
  8. fastjson将json字符串转化成map的五种方法
  9. 从Conficker蠕虫看AD帐号锁定
  10. excel下拉列表值的设定方式
  11. shell 小米system锁adb_忘记锁屏密码不用怕?支招小米手机解锁四种简单常用的方法...
  12. 一生一代一双人:我与51CTO学院的情缘----写于51CTO学院2周年庆
  13. Ztree Fa-Awesome 图标使用
  14. 自写:斤与公斤的转化
  15. eclipse 主类中明明有main方法且没有写成mian,还老是提示找不到main方法。
  16. 联想 S920 小白式一键ROOT工具
  17. ESP32 AT-MQTT 和阿里云进行数据传输
  18. The installation cannot continue as the installer file may be damaged. Download the installer file a
  19. 如何使用4EVERLAND-BUCKET获得免费云存储空间
  20. iOS暗黑(dark)模式适配

热门文章

  1. ruby和php比较,十张图读懂PHP、Python、 Ruby三大语言的差异
  2. 功能强大的截图软件——Snipaste
  3. 中学物理奥林匹克竞赛竞赛大纲
  4. 分享一款免费开源的在线文档管理插件
  5. 深信服售前产品经理面试准备材料(更新ing)
  6. 自己电脑中安装黑群辉NAS
  7. Windows Server 创建域、加入域、域管理
  8. 图片去除下间隙(图片3像素)的方法
  9. Angular中input双向绑定
  10. android 盒子刷机,一加5刷机盒子