Uva - 12506 - Shortest Names(Trip)

news/2024/11/30 18:45:54/

题意:有n个由小写字母组成的名字,任何一个名字都不是另一个名字的前缀,对于每个名字,只取它的最短前缀,使得这n个取出来的前缀互不相同,问这些前缀的总字母个数(1 <= n <= 1000, 所有名字的总长度不超过1000000,测试数据级数T <= 10)。

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3950

——>>看完题,想着这会不会是前缀树的题目,又看到名字长度不超过1000000,26^1000000个结点?于是不再考虑Trip。。。赛后才发现,看错题目了,1000000是所有名字加起来的最大总字母个数,正正是用Trip的题目。

设cnt[i]为第i个结点有多少个名字经过,那么,若cnt[i]为1,则表明只有1个名字经过,下面只表示1个人,于是可以停止往下的扫描;若cnt[i] > 1,则表明不止1个名字经过,必须往下扫描,直到能分辨出来为止。

Trip的数组写法:

#include <cstdio>
#include <cstring>
#include <queue>using namespace std;const int maxnode = 1000000 + 10;
const int maxn = 1000 + 10;
char name[maxn];
int n, ch[maxnode][26];
int cnt[maxnode];struct Trip{int sz;Trip(){sz = 1;memset(ch[0], 0, sizeof(ch[0]));memset(cnt, 0, sizeof(cnt));}int idx(char c){return c - 'a';}void insert(char *s){int len = strlen(s), u = 0, i;for(i = 0; i < len; i++){int c = idx(s[i]);if(!ch[u][c]){memset(ch[sz], 0, sizeof(ch[sz]));ch[u][c] = sz++;}cnt[ch[u][c]]++;u = ch[u][c];}}void solve(){int ret = 0, i, u;queue<int> qu;qu.push(0);while(!qu.empty()){u = qu.front(); qu.pop();for(i = 0; i < 26; i++) if(ch[u][i]){ret += cnt[ch[u][i]];if(cnt[ch[u][i]] > 1) qu.push(ch[u][i]);}}printf("%d\n", ret);}
};int main()
{int T;scanf("%d", &T);while(T--){Trip trip;scanf("%d", &n);for(int i = 0; i < n; i++){scanf("%s", name);trip.insert(name);}trip.solve();}return 0;
}

Trip的指针写法(事先不用预定空间,比较灵活):

#include <cstdio>
#include <cstring>
#include <queue>using namespace std;const int maxn = 1000 + 10;
char name[maxn];
int n;struct node{int cnt;node *next[26];node(){cnt = 0;memset(next, 0, sizeof(next));}
};struct Trip{node *root;Trip(){root = new node;}int idx(char c){return c - 'a';}void insert(char *s){int len = strlen(s), i;node *t = root;for(i = 0; i < len; i++){int c = idx(s[i]);if(!t->next[c]) t->next[c] = new node;t->next[c]->cnt++;t = t->next[c];}}void solve(){int ret = 0, i;node *t = new node;queue<node*> qu;qu.push(root);while(!qu.empty()){t = qu.front(); qu.pop();for(i = 0; i < 26; i++) if(t->next[i]){ret += t->next[i]->cnt;if(t->next[i]->cnt > 1) qu.push(t->next[i]);}}printf("%d\n", ret);}
};int main()
{int T;scanf("%d", &T);while(T--){Trip trip;scanf("%d", &n);for(int i = 0; i < n; i++){scanf("%s", name);trip.insert(name);}trip.solve();}return 0;
}




http://www.ppmy.cn/news/534220.html

相关文章

Shortest Names UVA - 12506

In a strange village, people have very long names. For example: aaaaa, bbb and abababab. You see, it’s very inconvenient to call a person, so people invented a good way: just call a prefix of the names. For example, if you want to call ‘aaaaa’, you can …

UVa 12506 Shortest Names

题目&#xff1a;uva.onlinejudge.org/index.php?optioncom_onlinejudge&Itemid8&pageshow_problem&problem3950 Description In a strange village, people have very long names. For example: aaaaa, bbb and abababab. You see, it’s very inconvenient to …

oracle登录12506,【案例】Oracle报错ORA-12560 sqlplus登录数据库时报错12560

天萃荷净 运维DBA反映,在sqlplus登录数据库时报错ORA-12560: TNS:protocol adapter error,分析原因为sqlplus版本导致 1.sqlplus登录数据库报ORA-12560 C:\Users\oracleplus>sqlplus / as sysdba SQL*Plus: Release 11.2.0.2.0 Production on Tue Feb 14 23:33:31 2012 Co…

UVA - 12506

Shortest Names 比赛时队友直接上的纯暴力&#xff0c;结果超时&#xff0c;我就想先排一下序&#xff0c;之后每次判断相邻的两个&#xff0c;实时更新每个字符串的喊出长度 #include <cstdio> #include <cstring> #include <iostream> #include <alg…

UVA12506 字典树简单应用

分析&#xff1a;构造字典树深搜 采用刘汝佳的左儿子右兄弟的构树方式&#xff0c;节省了大量的空间。 代码如下&#xff1a; #include <cstdio> #include <cmath> #include <cstring> using namespace std;typedef long long ll; const int maxn 1e610; i…

UVA 12506 Shortest Names

In a strange village, people have very long names. For example: aaaaa, bbb and abababab. You see, it’s very inconvenient to call a person, so people invented a good way: just call a prefix of the names. For example, if you want to call ‘aaaaa’, you can …

SpringBoot+JPA整合ShardingShpere实现分表-读写分离

ShardingShpere 分表分库读写分离 ​ ShardingShpere 提供来了根据某个字段分库分表的功能和读写分离。 读写分离 当主服务有写入&#xff08;insert/update/delete&#xff09;语句时&#xff0c;从服务器自动获取。 写入线程从 master 数据库查询查询线程从 salve 数据库…

oracle imp导入dmp文件报12506错误解决办法

通过imp导入数据库dmp文件出错解决办法&#xff1a; IMP-00058: 遇到 ORACLE 错误 12560 ORA-12560: TNS: 协议适配器错误 IMP-00000: 未成功终止导入 遇到这个问题后&#xff0c;很多人会认为是本地服务名的原因&#xff0c;但经过测试&#xff0c;发现服务名是正常的。如果服…