因为字符集比较大,所以就不能用简单字典树,在字典树里面,用链表进行存储。这个倒是不难,练了下手

统计的时候还是有点难搞,因为要算所有的两两比较的次数之和,对分叉处进行计算,注意细节

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = *+;
struct Trie
{
int head[N];
int next[N];
char ch[N];
int tot[N];
int sz;
long long ans;
void clear()
{
sz=;
head[]=next[]=tot[]=;
ans=;
}
void insert(const char* s)
{
int u=,v,len;
len=strlen(s);
tot[]++;
for (int i=; i<=len; i++)
{
bool found=false;
for (v=head[u]; v!=; v=next[v])
{
if (ch[v]==s[i])
{
found=true;
break;
}
}
if (!found)
{
v=sz++;
ch[v]=s[i];
tot[v]=;
next[v]=head[u];
head[u]=v;
head[v]=;
}
u=v;
tot[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]);
ans += sum / * ( * depth + );
for(int v = head[u]; v != ; v = next[v])
dfs(depth+, v);
}
} long long count()
{
ans = ;
dfs(, );
return ans;
}
} T;
char str[];
int main()
{
int n; int kase=;
while (scanf("%d",&n) && n)
{
T.clear();
for (int i=; i<=n; i++)
{
scanf("%s",str);
T.insert(str);
}
T.count();
printf("Case %d: %lld\n",++kase,T.ans);
}
return ;
}

UVA 11732 链表+字典树的更多相关文章

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

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

  2. UVA - 11488 字典树

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

  3. UVA 11488 Hyper Prefix Sets (字典树)

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

  4. HDU 1251 统计难题(字典树 裸题 链表做法)

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

  5. UVA - 12333 字典树+大数

    思路:用字典树将前40个数字记录下来,模拟大数加法即可. AC代码 #include <cstdio> #include <cmath> #include <algori ...

  6. UVA Phone List (字典树)(查询是否有前缀或自身是其他的前缀)

    Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16341   Accepted: 5228 Descr ...

  7. UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树

    题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨 ...

  8. uva 11488 - Hyper Prefix Sets(字典树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

  9. UVa 12333 - Revenge of Fibonacci manweifc(模拟加法竖式 & 字典树)

    题意: 给定n个(n<=40)数字, 求100000个以内有没有前面n个数字符合给定的数字的fibonacci项, 如果有, 给出最小的fibonacci项, 如果没有, 输出-1. 分析: 可 ...

随机推荐

  1. net.sf.json.JSONObject maven下载到了但是java后台一直用不了问题

    需求,实体转JSON,然后用到JSONObject转JSON,但是我向下面这样引入,后台就是用不了,还是报红, <dependency> <groupId>net.sf.jso ...

  2. Centos7安装mysql5.7.21

    1.卸载系统自带的 MariaDB [root@CentOS ~]# rpm -qa | grep mariadb mariadb-libs--.el7.x86_64 [root@CentOS ~]# ...

  3. 完整版excel上传导入读写批量数据并将反馈结果写入远程exel中

    思路:excel的读写借助于poi框架,在写入远程的时候,是不能直接写入的,本博主将传入的文件再次拉下来写到项目临时文件中,然后,在临时文件中写入,然后,以同样的名称路径覆盖掉远程的就可以了,稍微有点 ...

  4. redis3.2.2 集群

    http://blog.csdn.net/imxiangzi/article/details/52431729 http://www.2cto.com/kf/201701/586689.html me ...

  5. CSS的position属性:relative和absolute

    relative:是相对于自己来定位的,例如:#demo{position:relative;top:-50px;},这时#demo会在相对于它原来的位置上移50px.如果它之前的元素也为relati ...

  6. cf 785#

    23333再次水惨了..(心酸啊) A题呵呵呵呵呵 #include<cstdio> #include<iostream> using namespace std; int m ...

  7. 虚拟机安装centos6.5出现Error processing drive:pci-0000:00:10-scsi-0:0:0:0问题

    vmware安装linux系统出现Error processing drive:pci-0000:00:10-scsi-0:0:0:0问题 问题出现原因:我给虚拟机的内存太小了,只给了512M 解决办 ...

  8. 15.swoole学习笔记--异步写入文件

    <?php //异步写入文件 $content="hello world"; swoole_async_writefile('2.txt',$content,function ...

  9. 041-PHP把闭包函数当做参数传递

    <?php //把闭包函数当做参数传递 function demo($obj){ $obj('我爱PHP'); } # 传一个闭包过去 demo( function($txt){ echo $t ...

  10. 158-PHP strstr函数输出第一次出现字符串的位置到字符串结尾的所有字符串

    <?php $str='PHP is a very good programming language!'; //定义一个字符串 echo '第一次出现字母l的位置到字符串结尾的所有字符串'.s ...