zoukankan      html  css  js  c++  java
  • 第一轮 C

    最短的名字
    Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
    Submit Status
    
    Description
    Download as PDF
    
      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 ( T$ le$10), the number of test cases. Each test case begins with a line of one integer n ( 1$ le$n$ le$1000), 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 
    /*************************************************************************
    	> File Name: c.cpp
    	> Author:yuan 
    	> Mail: 
    	> Created Time: 2014年11月09日 星期日 09时44分06秒
     ************************************************************************/
    
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<cmath>
    #include<string>
    using namespace std;
    string str[1005];
    int t,n;
    bool check(string s,int num)
    {  
        int l=s.length();
       for(int i=0;i<n;i++)
        {
            if(i==num) continue;
            if(str[i].compare(0,l,s)==0) return 0;
        }
        return 1;
    }
    int main()
    {
        int ans;
        scanf("%d",&t);
        while(t--){
            ans=0;
            scanf("%d",&n);
            for(int i=0;i<n;i++)
            {
                cin>>str[i];
            }
            for(int i=0;i<n;i++)
              for(int j=1;j<=str[i].length();j++)
            {
                string s=str[i].substr(0,j);
                if(check(s,i)) {ans+=s.length();j=str[i].length()+1;}
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    


  • 相关阅读:
    USACO 玛丽卡(最短路+枚举)
    POJ 1161 Walls(最短路+枚举)
    Windows 上配置Docker Desktop 的k8s
    菜鸡学算法--70. 爬楼梯
    CLR 异步函数
    【.NET Core开发实战学习笔记】依赖注入框架:管理服务的依赖与生命周期
    【.NET Core 开发实战学习笔记】StartUp:理解程序的启动过程
    .ef core 多对对关系的关联方法
    HttpGet请求传递数组(集合)
    使用wkhtmltopdf工具生成pdf
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4254402.html
Copyright © 2011-2022 走看看