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. tab一些 添加 删除 搜索

    tab一些 添加 删除 搜索 案例 <!DOCTYPE html><html lang="en"><head> <meta charset ...

  2. iOS应用性能调优的4个建议和技巧

    任何一个能在用户手机屏幕中占有一席之地的iOS app都包含3个关键因素:想法好.设计出色.性能卓越.本文将分享一些iOS应用性能调优的4个建议和技巧. Tip #1:把图片资源压缩到最小.    i ...

  3. EXEC 和 SP_EXECUTESQL的区别

    摘要: MSSQL为我们提供了两种动态执行sql语句的命令:EXEC 和 SP_EXECUTESQL.通常SP_EXECUTESQL更具优势,因为它提供了输入输出的接口,且能够重用执行计划,大大提高执 ...

  4. NYOJ-915 +-字符串(贪心)

    +-字符串 时间限制:1000 ms  |  内存限制:65535 KB 难度:1   描述 Shiva得到了两个只有加号和减号的字符串,字串长度相同.Shiva一次可以把一个加号和它相邻的减号交换. ...

  5. k个区间相交的段落数 Educational Codeforces Round 4 D

    http://codeforces.com/contest/612/problem/D 题目大意:给你n个区间,这n个区间会有相交的部分,如果一个区间相交的部分>=k,那么就把这个区间记录下来. ...

  6. 插件管理工具 Alcatraz

    Alcatraz 安装: https://github.com/alcatraz/Alcatraz Github官网链接 终端安装方法 mkdir -p ~/Library/Application\ ...

  7. 十七、oracle 权限

    一.介绍这一部分我们主要看看oracle中如何管理权限和角色,权限和角色的区别在哪里.当刚刚建立用户时,用户没有任何权限,也不能执行任何操作.如果要执行某种特定的数据库操作,则必须为其授予系统的权限: ...

  8. ubuntu/linux 下 git 通过代理下载数据 (最简单的方式)

    git国内访问较慢,走代理较快. 方法:git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:port_n ...

  9. Android系统手机端抓包方法(tcpdump)

    抓包准备 1. Android手机需要先获得root权限.一种是否获得root权限的检验方法:安装并打开终端模拟器(可通过安卓市场等渠道获得).在终端模拟器界面输入su并回车,若报错则说明未root, ...

  10. UTF8,UTF16,UTF32,UTF16-LE,UTF16-BE,GBK 之间的转换

    Unicode是Unicode.org制定的编码标准,目前得到了绝大部分操作系统和编程语言的支持.Unicode.org官方对Unicode的定义是:Unicode provides a unique ...