input

n 2<=n<=4000

s1

s2

...

sn

1<=len(si)<=1000

output

输出用strcmp()两两比较si,sj(i!=j)要比较的次数,结果在long long范围内(相同字符比较两次,不相同字符比较一次,包括'\0')

做法:由于字符集太大,要用左兄弟右儿子的trie保存字符,不用每次都开ch[62]个孩子

 #include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#define MAX 100000
#define LL long long
#define mod 20071027
struct node
{
int sz;
char val;
node*ch[]; //ch[1]兄弟,ch[0]儿子
node()
{
ch[]=ch[]=NULL;
sz=;
}
};
char word[];
int n,cas=;
long long sum;
long long insert(char*s,node*u)
{
long long sum=,lastsz=u->sz++;
for(;*s||*(s-);s++)
{
if(!u->ch[]) //易错,要先把新建的结点连接到父结点才能往下走,否则新建之后父节点无法再读取到该结点
{
u->ch[]=new node;
u->ch[]->val=*s;
}
for(u=u->ch[];u->val!=*s;u=u->ch[])
{
if(!u->ch[])
{
u->ch[]=new node;
u->ch[]->val=*s;
}
}
sum+=lastsz+u->sz;
lastsz=u->sz++;
}
return sum;
}
/*//这样可以将父节点的ch和新建的结点绑定起来,传过来的是&root
long long insert(char*s,node**u)
{
long long sum=0,lastsz=(*u)->sz++;
for(;*s||*(s-1);s++)
{
printf("%p\n",u);
for(u=&((*u)->ch[0]);(*u)&&(*u)->val!=*s;u=&((*u)->ch[1]));
if(*u==NULL)
{
*u=new node;
(*u)->ch[0]=(*u)->ch[1]=NULL;
(*u)->val=*s;
}
sum+=lastsz+(*u)->sz;
lastsz=(*u)->sz++;
}
return sum;
}*/
void freenode(node*u)
{
if(u==NULL) return;
freenode(u->ch[]);
freenode(u->ch[]);
delete u;
}
int main()
{
//freopen("/home/user/桌面/in","r",stdin);
while(scanf("%d",&n)==&&n)
{
sum=;
node *root=new node;
while(n--)
{
scanf("%s",word);
sum+=insert(word,root);
}
printf("Case %d: %lld\n",cas++,sum);
freenode(root);
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return ;
} my Code

my Code

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; #define repf(i,a,b) for(int i=(a);i<=(b);i++)
typedef long long ll; const int N = ;
const int MAXNODE = ; int n, cas;
ll ans;
char str[]; struct STrie {
int son[MAXNODE];
int bro[MAXNODE];
int val[MAXNODE];
char ch[MAXNODE];
int sz; STrie() { sz = ; ch[] = val[] = bro[] = son[] = ; }
void init() { sz = ; ch[] = val[] = bro[] = son[] = ; }
// inline int idx(char c) { return c - 'a'; } void insert(char *s) {
int len = strlen(s), u = , p;
repf (i, , len) {
// check the brother of u
for (p = son[u]; p; p = bro[p]) {
if (ch[p] == s[i])
break;
}
// cannot find out than insert
if (!p) {
p = sz++;
ch[p] = s[i];
bro[p] = son[u];
son[p] = ;
val[p] = ;
son[u] = p;
}
ans += (val[u] - val[p]) * ( * i + );
if (len == i) {
ans += val[p] * ( * i + );
val[p]++;
}
val[u]++;
u = p;
}
}
} trie; int main() {
// ios_base::sync_with_stdio(0);
while (~scanf("%d", &n) && n) {
trie.init();
ans = ;
repf (i, , n - ) {
scanf("%s", str);
trie.insert(str);
}
printf("Case %d: %lld\n", ++cas, ans);
}
return ;
}

copy Code

UVA - 11732 "strcmp()" Anyone?左兄弟右儿子trie的更多相关文章

  1. 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?

    题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...

  2. UVA 11732 - strcmp() Anyone?(Trie)

    UVA 11732 - strcmp() Anyone? 题目链接 题意:给定一些字符串,要求两两比較,须要比較的总次数(注意.假设一个字符同样.实际上要还要和'\0'比一次,相当比2次) 思路:建T ...

  3. UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)

    题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...

  4. UVa 11732 strcmp()函数(左孩子右兄弟表示法)

    #include<iostream> #include<algorithm> #include<string> #include<cstring> #i ...

  5. UVA 11732 - strcmp() Anyone? 字典树

    传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  6. Uva 11732 strcmp() Anyone?

    strcmp() Anyone? Time Limit: 2000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Subm ...

  7. UVA 11732 strcmp() Anyone?(Trie的性质)

    strcmp() Anyone? strcmp() is a library function in C/C++ which compares two strings. It takes two st ...

  8. UVA - 11732 "strcmp()" Anyone? (trie)

    https://vjudge.net/problem/UVA-11732 题意 给定n个字符串,问用strcmp函数比较这些字符串共用多少次比较. strcmp函数的实现 int strcmp(cha ...

  9. UVA 11732 strcmp() Anyone? (压缩版字典树)

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

随机推荐

  1. HDU 1907 John(取火柴博弈2)

    传送门 #include<iostream> #include<cstdio> #include<cstring> using namespace std; int ...

  2. LeetCode OJ 154. Find Minimum in Rotated Sorted Array II

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  3. digitalocean注册验证账户、激活账号教程

    注册digitalocean账号很简单,使用优惠链接注册digitalocean还能赠送10美元余额,digitalocean vps是优秀的SSD VPS,最便宜的套餐只要5美元/月. 由于中国大陆 ...

  4. css样式的部分拓展, NuMber对象、 BoM、 DoM对象的模型的间述,ing...

    css部分代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...

  5. 比较不熟的JavaScript点滴,慢慢前行,附带简单复杂化的php小计算器一份

    interface.php <html> <head> <meta charset="utf-8" /> <title>这是一个简单 ...

  6. Openjudge-计算概论(A)-称体重

    描述: 赵.钱.孙.李四个人中既有大人也有小孩,给他们称体重时发现,他们每个人的体重都不一样,且体重(单位:公斤)恰好是10的整数倍,且他们的体重都不高 于50公斤,已知赵.钱两人的体重之和恰好等于孙 ...

  7. javascript显式类型的转换

    显式类型转换目的:为了使代码变得清晰易读,而做显示类型的转换常使用的函数:Boolean(),String(),Number()或Object()如:Nunber(5) //5String(true) ...

  8. Entity Framework Tools install to VS 2015

    因为在VS2013,2015里不再支持Sql compact 数据库的显示, 但是我们可以通过安装EF tools扩展来支持,参考地址:http://thedatafarm.com/data-acce ...

  9. UVALive - 3026 Period kmp next数组的应用

    input n 2<=n<=1000000 长度为n的字符串,只含小写字母 output Test case #cas 长度为i时的最小循环串 循环次数(>1) 若没有则不输出 做法 ...

  10. mac中Eclipse的快捷键

    查看某个类:command + shift +T 快速查看源代码中方法: command + o 选中某个类,command + t:查看此类的父类和子类 如果要导入一个类所在的包名,可以选中这个类, ...