博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 12506 Shortest Names
阅读量:4501 次
发布时间:2019-06-08

本文共 2580 字,大约阅读时间需要 8 分钟。

E - Shortest Names
Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit
 
Status
Description
  Shortest Names 
In a strange village, people have very long names. For example: aaaaa, bbb and abababab.
You see, it's very inconvenient to call a person, so people invented a good way: just call a prefix of the names. For example, if you want to call `aaaaa', you can call `aaa', because no other names start with `aaa'. However, you can't call `a', because two people's names start with `a'. The people in the village are smart enough to always call the shortest possible prefix. It is guaranteed that no name is a prefix of another name (as a result, no two names can be equal).
If someone in the village wants to call every person (including himself/herself) in the village exactly once, how many characters will he/she use?
Input
The first line contains T (T10), the number of test cases. Each test case begins with a line of one integer n ( 1n1000), the number of people in the village. Each of the following n lines contains a string consisting of lowercase letters, representing the name of a person. The sum of lengths of all the names in a test case does not exceed 1,000,000.
Output
For each test case, print the total number of characters needed.
Sample Input
1
3
aaaaa
bbb
abababab
Sample Output
5
Problemsetter: Rujia Liu, Special Thanks: Yiming Li, Feng Chen, Jane Alam Jan
字典树+节点记录
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=100000;
struct Trie
{
    int tot,root,ch[maxn][27];
    bool flag[maxn];
    int rk[maxn];
    Trie()
    {
        memset(ch[1],0,sizeof(ch[1]));
        memset(rk,0,sizeof(rk));
        flag[1]=false;
        root=tot=1;
        rk[1]=-1;
    }
    void Insert(const char*str)
    {
        int *cur=&root;
        for(const char* p=str;*p;p++)
        {
            cur=&ch[*cur][*p-'a'];
            rk[*cur]++;
            if(*cur==0)
            {
                tot++;
                *cur=tot;
                memset(ch[tot],0,sizeof(ch[tot]));
                flag[tot]=false;
                rk[tot]++;
            }
        }
        flag[*cur]=true;
    }
    int query(const char* str)
    {
        int first=1;
        int cnt=0;
        int *cur=&root;
        for(const char *p=str;*p && *cur;p++)
        {
            cur=&ch[*cur][*p-'a'];
            if(first)
            {
                if(rk[*cur]==1)
                {
                    first=0;
                }
                else
                {
                    cnt++;
                }
            }
        }
        //return (*cur&&flag[*cur]);
        return cnt;
    }
}tree[20];
char name[1010][10000];
int main()
{
    int T;
    cin>>T;
for(int t=0;t<T;t++)
{
    int n;
    cin>>n;
    int sum=0;
    for(int i=0;i<n;i++)
    {
        cin>>name
;
        tree[t].Insert(name
);
    }
    for(int i=0;i<n;i++)
    {
   //     cout<<name
<<"--->"<<tree[t].query(name)<<endl;
        sum+=tree[t].query(name
);
    }
/*
char  str[10000];
    while(cin>>str)
    {
        cout<<tree[t].query(str)<<endl;
    }
*/
    cout<<sum+n<<endl;
}
    return 0;
}

转载于:https://www.cnblogs.com/CKboss/p/3350948.html

你可能感兴趣的文章
【模板】并查集
查看>>
RabbitMQ使用教程(一)RabbitMQ环境安装配置及Hello World示例
查看>>
[WPF]实现密码框的密码绑定
查看>>
更新k8s镜像版本的三种方式
查看>>
WPF 获得当前输入法语言区域
查看>>
绑定元素属性改变不通知界面
查看>>
C#中使用反射获取结构体实例
查看>>
Spring bean的作用域和生命周期
查看>>
ado.net增删改查练习
查看>>
恩格尔系数
查看>>
纪检委,检察院的工资
查看>>
20135213 20135231 信息安全系统设计基础课程第一次实验报告
查看>>
BZOJ1419——Red is good(期望dp)
查看>>
Linux系统扩容根目录磁盘空间
查看>>
Java架构师书单
查看>>
二阶段冲刺第一天
查看>>
ArrayList删除特定元素的方法
查看>>
android 开发 View _15 导入一张图片将它裁剪成圆形 与 paint图层叠加处理详解
查看>>
地图大集合
查看>>
unity资源(移动版)提取 一点尝试
查看>>