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轮游戏,最后胜利的人是谁. 思路: 很 ...
随机推荐
- 【python】【转】python中isinstance判断变量类型用法
来源 http://www.jb51.net/article/15696.htm 在Python中只需要使用内置的函数isinstance,使用起来非常简单,比如下面的例子: 复制代码 代码如下: c ...
- C语言的编译过程和GCC编译参数
C语言的编译一般有三个步骤: 预编译: gcc -E -o a.e a.c 预编译a.c文件,生成的目标文件名为a.e 预编译就是将include包含的头文件内容替换到C文件中,同时删除代码中没用的注 ...
- C语言学习笔记(一):数组传递时退化为指针
这几天闲来无事,写了一个数组元素排序函数如下: #include <stdio.h> #include <stdlib.h> void ArraySort(int array[ ...
- 如何使用GCD(ZZ)
什么是GCD? Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法.该方法在Mac OS X 10.6雪豹中首次推出,并随后被引入到了iOS4 ...
- ibatis动态的传入表名、字段名
ibatis动态的传入表名.字段名,主要传入表名和字段名的不一致. Java代码: Map<String,Object> params = new HashMap<String,Ob ...
- Haskell高阶函数
Haskell functions can take functions as parameters and return functions as return values. A function ...
- 设计模式之(三)Proxy模式
今天学习Proxy模式.代理模式是在对已有对象操作困难或者不太方便时,选择用代理的方式对对象进行访问.Proxy实现的方法必须和被代理对象一致. 举一个简单的例子, 有一个Math类实现了IMath接 ...
- Learning WCF Chapter 3 Bindings One-Way and Duplex Communication
One-Way and Duplex Communication A message exchange pattern describes the way messages are sent betw ...
- vijosP1901学姐的钱包
题目:https://vijos.org/p/1901 题解:这题比较有意思. 经过一番思考之后我想出了下面的算法: 我们反着来推,按i从大到小 f[i]表示从>=m到 i 需要多长时间,则如果 ...
- Response.ContentType 详细列表 <转>
Response.ContentType 详细列表 不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 text/html 也就是网页格式.代码如: <% ...