Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable
题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化
思路:每一个单词除了首尾 中间的字符排序 然后插入字典树 记录每一个单词的数量
输入一个句子 每一个单词也排序之后查找 依据乘法原理 答案就是每一个单词的数量之积
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; const int maxnode = 111111;
const int sigma_size = 52;
int ch[maxnode][sigma_size];
int val[maxnode];
int sz;
void init()
{
sz = 1;
memset(ch[0], -1, sizeof(ch[0]));
}
int idx(char c)
{
if(c >= 'a' && c <= 'z')
return c - 'a';
else
return c - 'A' + 26;
} void insert(char *s)
{
int u = 0, n = strlen(s);
for(int i = 0; i < n; i++)
{
int c = idx(s[i]);
if(ch[u][c] == -1)
{
memset(ch[sz], -1, sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u]++;
}
int find(char *s)
{
int u = 0, n = strlen(s);
for(int i = 0; i < n; i++)
{
int c = idx(s[i]);
u = ch[u][c];
if(val[u] == -1)
return 0;
}
return val[u];
} int main()
{
int cas = 1;
int T;
scanf("%d", &T);
while(T--)
{
init();
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
char s[11111];
scanf("%s", s);
int len = strlen(s);
if(len > 2)
sort(s+1, s+len-1);
insert(s);
}
scanf("%d", &n);
getchar();
printf("Case %d:\n", cas++);
while(n--)
{
int sum = 1;
char s[11111];
gets(s);
char *p = strtok(s, " ");
while(p)
{
char str[11111];
strcpy(str, p);
int len = strlen(str);
if(len > 2)
sort(str+1, str+len-1);
sum *= find(str);
p = strtok(NULL, " ");
}
printf("%d\n", sum);
}
}
return 0;
}
Light OJ 1114 Easily Readable 字典树的更多相关文章
- 1114 - Easily Readable
1114 - Easily Readable PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB ...
- LeetCode OJ:Implement Trie (Prefix Tree)(实现一个字典树(前缀树))
Implement a trie with insert, search, and startsWith methods. 实现字典树,前面好像有道题做过类似的东西,代码如下: class TrieN ...
- SDUT OJ 迷之好奇 (字典树 )
迷之好奇 Time Limit: 2000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description FF得到了一个有n个数字的集 ...
- SDUT OJ 字典树 AND 静态内存与动态内存
字典树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 遇到单词不认识怎么办? 查字典 ...
- Trie(字典树,前缀树)_模板
Trie Trie,又经常叫前缀树,字典树等等. Trie,又称前缀树或字典树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的 ...
- Trie(前缀树/字典树)及其应用
Trie,又经常叫前缀树,字典树等等.它有很多变种,如后缀树,Radix Tree/Trie,PATRICIA tree,以及bitwise版本的crit-bit tree.当然很多名字的意义其实有交 ...
- 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)
前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- 字典树+博弈 CF 455B A Lot of Games(接龙游戏)
题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...
随机推荐
- Activity生命周期回顾
先来一张经典的生命周期图: ------------------------------------------------------------- 工程代码: ActivityLifeCycle. ...
- core文件找不到了
开始以为是core文件太大,设置ulimit -c unlimited 以后,再次访问,显示 ./a.out Segmentation fault (core dumped) 但是却找不到这个文件的 ...
- LightOj_1274 Beating the Dataset
题目链接 题意: 给一个文档, 这个文档由yes .no 组成, 共有s个byte, 共有n个yes.no. 假设yes的个数为yes_num, no的个数为no_num. 将这n个数进行排列, 对于 ...
- fixSidebar简介与修正log
fixSidebar是自己写的一个侧边栏固定小组件,主要是用于主内容较长时让侧边栏能保持显示,依赖于jQuery. Github地址 https://github.com/iRuxu/fixSideb ...
- nutch 1.7 修改代码后如何编译发布,并集群采集攻略
nutch 1.3之后,分布式的可执行文件与单机可执行文件进行了分离 接上篇,nutch 1.7 导入 eclipse 本篇所要解决的问题:nutch下载下来经过简单的配置即可进行采集,但有时候我们需 ...
- jquery deferred
http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html http:// ...
- 如何通过js使搜索关键词高亮
给你推荐通过jquery来实现高亮关键词.jquery.textSearch-1.0.js代码: (function($){ $.fn.textSearch =function(str,options ...
- 关于sql语句in的使用注意规则( 转)
select * from tuser where userno not in(select userno from filter_barcode) 上面这条语句子查询里,userno 并不存在fil ...
- c++ 名字粉碎(name mangling)
转自Ibm: Name mangling is the encoding of function and variable names into unique names so that linker ...
- 正在执行的SQL和之前执行的SQL
SQL> select * from v$mystat where rownum<2; SID STATISTIC# VALUE ---------- ---------- ------- ...