uva11732:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=2832&mosmsg=Submission+received+with+ID+13889354

题意:给你n个串,串之间两两进行比较,计算字符被比较的总次数。

题解:这里注意,'\0'也会被比较。这一题,看的是刘汝佳的代码,才知道怎么统计比较的次数。下面的代码,是刘汝佳的代码。

 #include<cstring>
#include<vector>
using namespace std;
const int maxnode = * + ;
const int sigma_size = ;
// 字母表为全体小写字母的Trie
struct Trie {
int head[maxnode]; // head[i]为第i个结点的左儿子编号
int next[maxnode]; // next[i]为第i个结点的右兄弟编号
char ch[maxnode]; // ch[i]为第i个结点上的字符
int tot[maxnode]; // tot[i]为第i个结点为根的子树包含的叶结点总数
int sz; // 结点总数
long long ans; // 答案
void clear() {
sz = ;
tot[] = head[] = next[] = ;
} // 初始时只有一个根结点 // 插入字符串s(包括最后的'\0'),沿途更新tot
void insert(const char *s) {
int u = , v, n = strlen(s);
tot[]++;
for(int i = ; i <= n; i++) {
// 找字符a[i]
bool found = false;
for(v = head[u]; v != ; v = next[v])
if(ch[v] == s[i]) { // 找到了
found = true;
break;
}
if(!found) {
v = sz++; // 新建结点
tot[v] = ;
ch[v] = s[i];
next[v] = head[u];
head[u] = v; // 插入到链表的首部
head[v] = ;
}
u = v;
tot[u]++;
}
} // 统计LCP=u的所有单词两两的比较次数之和
void dfs(int depth, int u) {
if(head[u] == ) // 叶结点
ans += tot[u] * (tot[u] - ) * depth;
else {
int sum = ;
for(int v = head[u]; v != ; v = next[v])
sum += tot[v] * (tot[u] - tot[v]); // 子树v中选一个串,其他子树中再选一个
ans += sum / * ( * depth + ); // 除以2是每种选法统计了两次
for(int v = head[u]; v != ; v = next[v])
dfs(depth+, v);
}
} // 统计
long long count() {
ans = ;
dfs(, );
return ans;
}
};
#include<cstdio>
const int maxl = + ; // 每个单词最大长度
int n;
char word[maxl];
Trie trie;
int main() {
int kase = ;
while(scanf("%d", &n) == && n) {
trie.clear();
for(int i = ; i < n; i++) {
scanf("%s", word);
trie.insert(word);
}
printf("Case %d: %lld\n", kase++, trie.count());
}
return ;
}

strcmp() Anyone?的更多相关文章

  1. Uva 11732 strcmp() Anyone?

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

  2. Linux C 字符串函数 strlen()、strcat()、strncat()、strcmp()、strncmp()、strcpy()、strncpy() 详解

      strlen(返回字符串长度) 表头文件 #include <string.h> 定义函数 size_t strlen(const char *s); 函数说明 strlen()用来计 ...

  3. strcmp

     C++ Code  123456789101112   int strcmp(const char *dest, const char *source) {     assert((NULL !=  ...

  4. strlen(); strcpy(); strcat(); strcmp() ---笔记

    指针小知识点: int a =10; int *p=&a; int *q=p;        //p中保存的是a的地址 int *q=p;       //将p的值赋给q 作用是让q也指向a ...

  5. Strcmp(字符串1,字符串2)函数 Sizeof && strlen() Substr(a,b)

    Strcmp(字符串1,字符串2)函数 { strcmp函数是比较两个字符串的大小,返回比较的结果.一般形式是:  i=strcmp(字符串,字符串); 其中,字符串1.字符串2均可为字符串常量或变量 ...

  6. strcmp()&&mb_ereg_replace()&&ereg()

    主要记录两个函数,一个是strcmp(),一个是mb_ereg_replace() strcmp() php 5.3 以后字符串和数组比较会返回0 测试代码: PHP <?php $passwo ...

  7. strcmp函数使用总结

    Action() { /********************************* * Author:旺仔 * object:strcmp * date:2015-12-09 * fuc:我输 ...

  8. strcmp函数的使用

    Action() { /*********************************   * Author:旺仔   * object:strcmp   * date:2015-12-09    ...

  9. c/c++面试题(3)strcat/strcmp/strlen/strcpy的实现

    1.编写一个函数实现strlen以及strcpy函数. strcpy函数. 后面的字符串拷贝到一个字符数组中,要求拷贝好的字符串在字符数组的首 地址,并且只拷贝到'\0'的位置.原型是 char* m ...

  10. strcmp传入nil导致崩溃

    现象:连接电脑可以正常启动程序,不连接电脑启动程序就崩溃. 崩溃信息: BSXPCMessage received error for message: Connection invalid HW k ...

随机推荐

  1. 3rd day

    今天学习创建了几个简单的表:

  2. HTML5 骨架

    html: <!DOCTYPE html> <html lang="zh-CN"> <head> <title>HTML5 骨架&l ...

  3. python获取网络时间和本地时间

    今天我们来看一下如何用python获取网络时间和本地时间,直接上代码吧,代码中都有注释. python获取网络时间 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  4. NOI2015 程序自动分析 prog

    何等水题 某神犇仿关押罪犯的写法 却写挂了  然而实际上并不需要补集之类的 #include<iostream> #include<cstring> #include<c ...

  5. Java 判断一段网络资源是否存在

    package cn.ycmedia.common.utils; import java.io.InputStream; import java.net.URL; import java.net.UR ...

  6. VC++获取网卡MAC、硬盘序列号、CPU ID、BIOS编号

    以下代码可以取得系统特征码(网卡MAC.硬盘序列号.CPU ID.BIOS编号) BYTE szSystemInfo[4096]; // 在程序执行完毕后,此处存储取得的系统特征码 UINT uSys ...

  7. iOS中sqlite3操作

    声明:下面命令我没有所有使用过, 仅用于收藏, 欢迎大家指出当中的错误 'SELECT  count(*)   FROM sqlite_master WHERE type="table&qu ...

  8. HDU 3533 Escape(bfs)

    Escape Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  9. codevs1226倒水问题(Bfs)

    /* 首先建立模型 可以看成是三个水杯 第三个无穷大 (这里看成是201足够了) 最少步数 想到Bfs 维护队列里的状态:要有个步数 还要有v :此时刻三个杯子有多少水 然后倒水:因为没有刻度 所以有 ...

  10. 重装系统时,将MBR分区转为GPT 分区

    摘要 很多同学在重装系统的时候,或多或少都遇到过这样的问题:镜像文件没有问题,软碟通刻录也没有问题,但偏偏就在选择安装系统盘盘符的时候,跳出对话框,提示:Windows无法安装到这个磁盘,选中的磁盘具 ...