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. JPA query 基本语法解释

    详细语法官网去学习 -->> http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference Qu ...

  2. OSChina 其中很重要的一类——RequestContext

    RequestContext 这个类在 OSChina 中是很重要的一个类.该类由全局 Filter 进行初始化.并传递给包含 Action 和 页面中直接使用.使用时通过 RequestContex ...

  3. sbt 配置

    1. SBT使用local maven repository,下载的库依然放在 ~/.m2/repository 2. SBT assembly 会把依赖库打包到一个jar包,需要使用assembly ...

  4. Android(java)学习笔记235:多媒体之计算机图形表示方式

    1.多媒体 很多媒体:文字(TextView,简单不讲),图片,声音,视频等等.   2.图片 计算机如何表示图片的? (1)bmp 高质量保存    256色位图:图片中的每个像素点可以有256种颜 ...

  5. Android WindowManager的使用

    经常,我们看到在桌面上可移动的悬浮窗,这种场景还是很多的, 像流量统计,桌面歌词等,安全软件的清理小部件 这种小部件主要是通过 WindowManager ; WindowManager.Layout ...

  6. Android Camera开发:使用GLSurfaceView预览Camera 基础拍照

    GLSurfaceView是OpenGL中的一个类,也是可以预览Camera的,而且在预览Camera上有其独到之处.独到之处在哪?当使用Surfaceview无能为力.痛不欲生时就只有使用GLSur ...

  7. servlet 配置到服务器

    最近写了个安卓项目,服务端用的servlet.因为第一次写java项目,写完如何发布不是太清除,于是把这回经理写出来,一来做个记录,二来也给和我同样经历的朋友一点启示. 首先配置你的java主机和你的 ...

  8. 关于c#流

     C#流的简单认识 前言 本篇文章简单总结了在C#编程中经常会用到的一些流.比如说FileStream.MemoryStream. BufferedStream. NetWorkStream. Str ...

  9. PC端手机访问跳转手机站点

    第一种: var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.sr ...

  10. asp.net 从客户端中检测到有潜在危险的 Request.Form 值错误解

    从客户端(ftbContent="<P><A href="http://l...")中检测到有潜在危险的 Request.Form 值. 说明: 请求验 ...