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_4918_Query on the subtree(树的分治+树状数组)

    题目链接:hdu_4918_Query on the subtree 题意: 给出一颗n个点的树,每个点有一个权值,有两种操作,一种是将某个点的权值修改为v,另一种是查询距离点u不超过d的点的权值和. ...

  2. Haskell开始

    一.安装 操作系统为centos,为了安装最新的Haskell编译器,不使用命令 yum install ghc 1.安装ghc 使用wget下载ghc,命令如下(注意系统是centos6.7) $ ...

  3. LeetCode OJ 33. Search in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  4. 新版本的jquery checkbox 全选反选代码只能执行一遍,第二次就失败attr与prop区别

    $("#all_check").click(function() { $("input[name='checkShop[]']").attr("che ...

  5. 38.利用接口做参数,写个计算器,能完成+-*/运算 (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 (3)设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) 此方法要求能够:1.用传递过来的对象调用comp

    //接口Compute package jieKou; public interface Compute { int Computer(int n,int m); } //加 package jieK ...

  6. Alyona and flowers

    Alyona and flowers time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. Learning BSD.sys/queue.h

    This file includes 4 data-structures.. Insteresting because they are written in 1994.. to make it ea ...

  8. JAVA常识积累

    java Class<? extends Object> getClass() 这么写是什么意思 Class<? extends Object> 泛型Object以及Objec ...

  9. Screen tearing

    Umm, screen tearing happens when the frame rate and the monitor refresh rate don't match.  When that ...

  10. iosOC不可变数组遍历

    NSArray * array = @[@"1",@"2",@"3"]; NSLog(@"%@",array); //1 ...