Trie UVA 11732 "strcmp()" Anyone?
题意:询问所有字符串的比较次数和(注意for循环内的比较也算)
分析:将所有字符串插入到字典树上,然后结点信息记录有几个字符串,那么每走到一个结点就能知道比较到此时需要的次数。学习到链表存结点
#include <bits/stdc++.h>
using namespace std; typedef long long ll;
const int N = 4e3 + 5;
const int M = 1e3 + 5;
const int NODE = N * M;
struct Trie {
int head[NODE], nex[NODE];
char ch[NODE];
int cnt[NODE];
int sz;
void clear() {
sz = 1; cnt[0] = head[0] = nex[0] = 0;
}
void insert(char *str) {
int u = 0, len = strlen (str);
cnt[0]++;
for (int i=0; i<=len; ++i) {
bool found = false;
int v;
for (v=head[u]; v; v=nex[v]) {
if (ch[v] == str[i]) {
found = true; break;
}
}
if (!found) { //插入到链表的头结点后一个
v = sz++;
cnt[v] = 0;
ch[v] = str[i];
nex[v] = head[u];
head[u] = v;
head[v] = 0;
}
u = v; cnt[u]++;
}
}
void query(int u, int dep, ll &res) {
if (!head[u]) {
res += cnt[u] * (cnt[u] - 1) * dep;
return ;
}
int sum = 0, tmp = cnt[u];
for (int v=head[u]; v; v=nex[v]) {
//sum += cnt[v] * (cnt[u] - cnt[v]);
res += cnt[v] * (tmp - cnt[v]) * (dep * 2 + 1);
tmp -= cnt[v];
}
//res += sum / 2 * (2 * dep + 1);
for (int v=head[u]; v; v=nex[v]) {
query (v, dep + 1, res);
}
}
}trie;
char word[M]; int main(void) {
int n, cas = 0;
while (scanf ("%d", &n) == 1) {
if (!n) break;
trie.clear ();
for (int i=0; i<n; ++i) {
scanf ("%s", &word);
trie.insert (word);
}
ll ans = 0;
trie.query (0, 0, ans);
printf ("Case %d: %lld\n", ++cas, ans);
} return 0;
}
Trie UVA 11732 "strcmp()" Anyone?的更多相关文章
- 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?
题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...
- UVA 11732 - strcmp() Anyone?(Trie)
UVA 11732 - strcmp() Anyone? 题目链接 题意:给定一些字符串,要求两两比較,须要比較的总次数(注意.假设一个字符同样.实际上要还要和'\0'比一次,相当比2次) 思路:建T ...
- UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)
题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...
- UVA 11732 strcmp() Anyone?(Trie的性质)
strcmp() Anyone? strcmp() is a library function in C/C++ which compares two strings. It takes two st ...
- UVA - 11732 "strcmp()" Anyone?左兄弟右儿子trie
input n 2<=n<=4000 s1 s2 ... sn 1<=len(si)<=1000 output 输出用strcmp()两两比较si,sj(i!=j)要比较的次数 ...
- UVA - 11732 "strcmp()" Anyone? (trie)
https://vjudge.net/problem/UVA-11732 题意 给定n个字符串,问用strcmp函数比较这些字符串共用多少次比较. strcmp函数的实现 int strcmp(cha ...
- uva 11732 - strcmp() Anyone? 不错的Trie题
题解:http://blog.csdn.net/u013480600/article/details/23122503 我的代码一直TLE,,,看了人家的之后,认为1.链式前向星比較好,2.*dept ...
- UVA 11732 strcmp() Anyone (Trie+链表)
先两两比较,比较次数是两者相同的最长前缀长度*2+1,比较特殊的情况是两者完全相同时候比较次数是单词长度*2+2, 两个单词'末尾\0'和'\0'比较一次,s尾部'\0'和循环内'\0'比较一次. 因 ...
- UVA 11732 - strcmp() Anyone? 字典树
传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
随机推荐
- GridView如何合并同类项
/// <summary> /// 合并GridView中某列相同信息的行(单元格) /// </summary> /// <param name ...
- yii 自定义组件的调用
1,main.php 里面导入 'import' => array( 'application.components.*' ), 2,application/ ...
- 获取driver网络路径名称
'get the web path of the drive s: Dim MM As New Management.ManagementObject(String.Format("win3 ...
- Server.Transfer方式(或称HttpContext方式)传值实例
public class QueryPage : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox txtStaDate ...
- Cocoapods的安装报错 - Error installing pods:activesupport requires Ruby version >=2.2.2
1.打开终端 2 移除现有 Ruby 默认源 输入以下指令 $gem sources --remove https://rubygems.org/ 3.使用新的源 输入以下指令 $gem source ...
- repo 版本回退
转自:http://blog.csdn.net/wed110/article/details/52179386 1.repo 回退到具体某一天的提交 repo forall -c 'ID=`Git l ...
- 【转载】PHP使用1个crontab管理多个crontab任务
转载来自: http://www.cnblogs.com/showker/archive/2013/09/01/3294279.html http://www.binarytides.com/php- ...
- hdu 4277 2012长春赛区网络赛 dfs+hashmap ***
hashmap判重大法好 #include<cstdio> #include<iostream> #include<algorithm> #include<c ...
- 10g ASM下修改control file的位置
1.查看位置以及name是否正确 SQL> sho parameter name NAME TYPE VALUE ------------------------------------ --- ...
- 无法打开包括文件:“windows.h”: No such file or directory
VS2012 出现如下错误: 无法打开包括文件:"windows.h": No such file or directory 解决办法,将 C:\Program Files ...