gym 100935B (字典树)
2016-03-24 23:32
344 查看
B. Weird Cryptography
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output
Khaled was sitting in the garden under an apple tree, suddenly! , well... you should guess what happened, an apple fell on his head! , so he came up with a new Cryptography method!! The method deals only with numbers, so... If you want to encode a number, you
must represent each of its digits with a set of strings, then the size of the set is the digit itself, No set should contain the same string more than once. For example: the number 42, can be represented with the following two sets: 1) "dog" "load" "under"
"nice". 2) "stack" "dog". The first set contain four strings so it represent the digit 4. The second set contain two strings so it represent the digit 2. Given N strings, what is the smallest number you can get from dividing these strings into non-empty
sets, and then decode the result by Khaled's Cryptography method? , You must use all the given strings, and no set should contain the same string more than once.
Input
The input consists of several test cases, each test case starts with 0 < N ≤ 10000, the number of the given strings, then follows N space-separated
string, each string will contain only lower-case English letters, and the length of each string will not exceeded 100. You can assume that there are no more than nine distinct strings among the given strings. A line containing the number 0 defines the end
of the input you should not process this line.
Output
For each test case print a single line in the following format: "Case c: x" where c is the test case number starting from 1 and x is the solution to the described problem above.
Examples
input
output
Note
In the first sample, we divided the given strings into two sets, the first set contains two word: "one" and "two" so it represents the digit 2, the second set contains only one word: "two" so it represent the digit 1.
题意:一个集合的size表示一个数字,把n个字符串放到不同的集合,一个集合中不能出现相同的字符串,求用
每个集合的size表示的数字组成的数的最小值.
开一个字典树节点统计从根到这个节点为末尾的字符串个数,然后最后统计所有的节点数字,每次把数组
贪心减一.
#include <bits/stdc++.h>
using namespace std;
#define maxn 11111
int n;
struct node {
int num;
int next[26];
}tree[maxn<<4];
char a[maxn];
int root, cnt;
void build_node (int u) {
for (int i = 0; i < 26; i++)
tree[u].next[i] = -1;
tree[u].num = 0;
}
void add () {
int l = strlen (a);
int p = root;
for (int i = 0; i < l; i++) {
int id = a[i]-'a';
if (tree[p].next[id] == -1) {
build_node (cnt);
tree[p].next[id] = cnt++;
}
if (i == l-1) {
tree[tree[p].next[id]].num++;
}
p = tree[p].next[id];
}
}
int ans[maxn], ans_cnt;
void dfs (int u) {
if (tree[u].num > 0) {
ans[ans_cnt++] = tree[u].num;
}
for (int i = 0; i < 26; i++) {
if (tree[u].next[i] != -1) {
dfs (tree[u].next[i]);
}
}
return ;
}
void solve () {
ans_cnt = 0;
dfs (root);
sort (ans, ans+ans_cnt);
char gg[maxn];
int gg_cnt = 0;
int pos = 0;
while (pos < ans_cnt) {
gg[gg_cnt++] = ans_cnt-pos+'0';
for (int i = pos; i < ans_cnt; i++) {
ans[i]--;
if (ans[i] == 0)
pos++;
}
}
for (int i = gg_cnt-1; i >= 0; i--) {
printf ("%c", gg[i]);
}
printf ("\n");
}
int main () {
//freopen ("in.txt", "r", stdin);
int kase = 0;
while (scanf ("%d", &n) == 1 && n) {
cnt = 1;
root = 0;
build_node (root);
for (int i = 0; i < n; i++) {
scanf ("%s", a);
add ();
}
printf ("Case %d: ", ++kase);
solve ();
}
return 0;
}
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output
Khaled was sitting in the garden under an apple tree, suddenly! , well... you should guess what happened, an apple fell on his head! , so he came up with a new Cryptography method!! The method deals only with numbers, so... If you want to encode a number, you
must represent each of its digits with a set of strings, then the size of the set is the digit itself, No set should contain the same string more than once. For example: the number 42, can be represented with the following two sets: 1) "dog" "load" "under"
"nice". 2) "stack" "dog". The first set contain four strings so it represent the digit 4. The second set contain two strings so it represent the digit 2. Given N strings, what is the smallest number you can get from dividing these strings into non-empty
sets, and then decode the result by Khaled's Cryptography method? , You must use all the given strings, and no set should contain the same string more than once.
Input
The input consists of several test cases, each test case starts with 0 < N ≤ 10000, the number of the given strings, then follows N space-separated
string, each string will contain only lower-case English letters, and the length of each string will not exceeded 100. You can assume that there are no more than nine distinct strings among the given strings. A line containing the number 0 defines the end
of the input you should not process this line.
Output
For each test case print a single line in the following format: "Case c: x" where c is the test case number starting from 1 and x is the solution to the described problem above.
Examples
input
3 one two two 7 num go book go hand num num 25 aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa 0
output
Case 1: 12 Case 2: 124 Case 3: 1111111111111111111111111
Note
In the first sample, we divided the given strings into two sets, the first set contains two word: "one" and "two" so it represents the digit 2, the second set contains only one word: "two" so it represent the digit 1.
题意:一个集合的size表示一个数字,把n个字符串放到不同的集合,一个集合中不能出现相同的字符串,求用
每个集合的size表示的数字组成的数的最小值.
开一个字典树节点统计从根到这个节点为末尾的字符串个数,然后最后统计所有的节点数字,每次把数组
贪心减一.
#include <bits/stdc++.h>
using namespace std;
#define maxn 11111
int n;
struct node {
int num;
int next[26];
}tree[maxn<<4];
char a[maxn];
int root, cnt;
void build_node (int u) {
for (int i = 0; i < 26; i++)
tree[u].next[i] = -1;
tree[u].num = 0;
}
void add () {
int l = strlen (a);
int p = root;
for (int i = 0; i < l; i++) {
int id = a[i]-'a';
if (tree[p].next[id] == -1) {
build_node (cnt);
tree[p].next[id] = cnt++;
}
if (i == l-1) {
tree[tree[p].next[id]].num++;
}
p = tree[p].next[id];
}
}
int ans[maxn], ans_cnt;
void dfs (int u) {
if (tree[u].num > 0) {
ans[ans_cnt++] = tree[u].num;
}
for (int i = 0; i < 26; i++) {
if (tree[u].next[i] != -1) {
dfs (tree[u].next[i]);
}
}
return ;
}
void solve () {
ans_cnt = 0;
dfs (root);
sort (ans, ans+ans_cnt);
char gg[maxn];
int gg_cnt = 0;
int pos = 0;
while (pos < ans_cnt) {
gg[gg_cnt++] = ans_cnt-pos+'0';
for (int i = pos; i < ans_cnt; i++) {
ans[i]--;
if (ans[i] == 0)
pos++;
}
}
for (int i = gg_cnt-1; i >= 0; i--) {
printf ("%c", gg[i]);
}
printf ("\n");
}
int main () {
//freopen ("in.txt", "r", stdin);
int kase = 0;
while (scanf ("%d", &n) == 1 && n) {
cnt = 1;
root = 0;
build_node (root);
for (int i = 0; i < n; i++) {
scanf ("%s", a);
add ();
}
printf ("Case %d: ", ++kase);
solve ();
}
return 0;
}
相关文章推荐
- 虚幻4目录结构
- IOS学习笔记 ——生命周期
- Uva160——Factors and Factorials
- zlib1.dll之64位下载与生成的两种方法
- WinCE项目应用之车载导航
- nefu 1029 字符串
- 转:python 多线程就这么简单
- iOS 开发中的一些注意点(安全、当前语言、时间格式化)
- 【杭电oj】2647 - Reward(拓扑排序)(含测试数据)
- 技术文章阅读记录
- JAVA代码实现FTP文件下载
- C提高——结构体
- GitHub 上排名前 100 的 Objective-C 项目简介
- 类型自动转换引起的误解——QString可以赋值数字1,也能直接与0比较,真是昏倒!
- 【C#进阶系列】16 数组
- [ 脚本 ] RHEL6.x 及Centos6.x 初始化脚本
- uva 129 krypton factors ——yhx
- 【C#进阶系列】15 数组
- 智能指针(模拟实现auto_ptr,shared_ptr,scooeptr 以及定制删除器c++ 实现)
- 删除a3.txt文件中含dong的行