Flying to the Mars(字典树)

2023-11-05

Flying to the Mars

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12965    Accepted Submission(s): 4111


Problem Description

In the year 8888, the Earth is ruled by the PPF Empire . As the population growing , PPF needs to find more land for the newborns . Finally , PPF decides to attack Kscinow who ruling the Mars . Here the problem comes! How can the soldiers reach the Mars ? PPF convokes his soldiers and asks for their suggestions . “Rush … ” one soldier answers. “Shut up ! Do I have to remind you that there isn’t any road to the Mars from here!” PPF replies. “Fly !” another answers. PPF smiles :“Clever guy ! Although we haven’t got wings , I can buy some magic broomsticks from HARRY POTTER to help you .” Now , it’s time to learn to fly on a broomstick ! we assume that one soldier has one level number indicating his degree. The soldier who has a higher level could teach the lower , that is to say the former’s level > the latter’s . But the lower can’t teach the higher. One soldier can have only one teacher at most , certainly , having no teacher is also legal. Similarly one soldier can have only one student at most while having no student is also possible. Teacher can teach his student on the same broomstick .Certainly , all the soldier must have practiced on the broomstick before they fly to the Mars! Magic broomstick is expensive !So , can you help PPF to calculate the minimum number of the broomstick needed .
For example : 
There are 5 soldiers (A B C D E)with level numbers : 2 4 5 6 4;
One method :
C could teach B; B could teach A; So , A B C are eligible to study on the same broomstick.
D could teach E;So D E are eligible to study on the same broomstick;
Using this method , we need 2 broomsticks.
Another method:
D could teach A; So A D are eligible to study on the same broomstick.
C could teach B; So B C are eligible to study on the same broomstick.
E with no teacher or student are eligible to study on one broomstick.
Using the method ,we need 3 broomsticks.
……

After checking up all possible method, we found that 2 is the minimum number of broomsticks needed. 
 

Input
Input file contains multiple test cases. 
In a test case,the first line contains a single positive number N indicating the number of soldiers.(0<=N<=3000)
Next N lines :There is only one nonnegative integer on each line , indicating the level number for each soldier.( less than 30 digits);
 

Output
For each case, output the minimum number of broomsticks on a single line.
 

Sample Input
  
  
4 10 20 30 04 5 2 3 4 3 4
 

Sample Output
  
  
1 2
 
上篇那个用map过得太过偶然,因为数字太大(最多30位)整
数撑不下!当然了用map<string,int>就可以过了,但是正好学
到字典树,用字典树试一下效果相当的好!得意大赞!
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
struct Tree
{
    int num;
    Tree *next[10];
    Tree()
    {
        num=0;
        for(int i=0;i<10;i++)
        {
            next[i]=NULL;
        }
    }
}*root;
int insert(Tree *p,char *s)
{
    int i=0;
    while(s[i])
    {
        int x=s[i]-'0';
        if(p->next[x]==NULL)
        {
            p->next[x]=new Tree();
        }
        p=p->next[x];
        i++;
    }
    p->num++;
   // cout<<"数为:"<<p->num<<endl;
    return (p->num);
}
int main()
{
    char s[30],s1[30];
    int n,maxs,stl,i,len;
    while(cin>>n)
    {
        root=new Tree();
        maxs=-5;
        while(n--)
        {
            cin>>s;
            stl=strlen(s);
            i=len=0;
            for(i=0;i<stl;i++)
            {
                if(s[i]!='0')
                {
                    break;
                }
            }
            for(i;i<stl;i++)
            {
                s1[len++]=s[i];
            }
            s1[len]='\0';
          //  cout<<s1<<endl;
            maxs=max(maxs,insert(root,s1));
        }
        cout<<maxs<<endl;
    }
}
num是在每一个已经装好的字符串后面记录一下,正好记录的是
这个字符串在文中的次数!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Flying to the Mars(字典树) 的相关文章

随机推荐

  • cesium 如何使实体平滑更新位置

    如果需要不断更新实体位置 实现平滑过渡的效果可以借鉴该方式 两种方式实现 一是直接赋值新坐标位置 但有时会出现闪烁情况 这里推荐第二种 通过回调函数的方式更新位置 1 直接赋值方式 直接赋值方式 cesium绘制原理是先移除 然后在新位置渲
  • 论文写作资源整理

    论文写作及实验资源 文章目录 论文写作及实验资源 实验相关 数据集 样例代码 在线训练平台 写作辅助 文献管理 文档阅读 图表绘制 文档写作 降重查重 英文写作 其它工具 文献分类 文献检索 期刊下载 顶级会议 信息安全会议 计算机视觉会议
  • Vue-组件二次封装

    本次对el input进行简单封装进行演示 封装很简单 就给激活样式的边框 主要是功能 本次封装主要使用到vue自带的几个对象 attrs 获取绑定在组件上的所有属性 listeners 获取绑定在组件上的所有函数方法 slots 获取应用
  • String arg = input.nextLine();为什么不执行

    String a in nextLine 和String a in next 的区别 当发现String a in nextLine 不能按照自己的要求执行时 可以换为String a in next 执行 nextline读取到的是换行符
  • 06_Vue-router与综合练习

    Vue router 一 生命周期钩子函数 含义 在生命周期处理响应函数的别称 1 初始化 beforeCreat 创建对象时 没初始化data和methods created 实例已经创建好了 此时在里面发送ajax请求 2 挂载 bef
  • 自学Python兼职赚钱靠谱吗?

    自学python兼职九成九是赚不到钱的 程序员兼职的门槛是挺高的 python兼职的类型可以分为 开发 也就是写网页的底层逻辑 但是大概率需要会前端 前端也就是页面 爬虫 数据分析 兼职的话 甲方是不会把数据给你去分析的 在这个社会当中数据
  • html动态设置透明度

  • Debian12中为python3配置虚拟环境及在Pycharm中使用虚拟环境

    在Debian 12中 python默认为python 3 11 基于应用 现需设置虚拟环境 1 安装venv模块 从python3 3开始 配置python虚拟环境 可用venv模块 更加方便了 执行命令 apt install pyth
  • 网络安全管理

    网络安全面临的主要威胁 人为因素 系统和运行环境等 常见的互联网服务安全包括 Web浏览器安全 文件传输 FTP 服务安全 E mail服务安全 远程登录 Telnet 安全 DNS域名安全和设备的实体安全 防火墙的局限性以及风险 防火墙能
  • 编译和安装gdb源码详细步骤介绍

    1 gdb源码下载 1 源码下载网址 https ftp gnu org gnu gdb 2 本文下面的编译是按照8 2版本的源码进行的 其余版本的源码可能会报错 需要自行解决 2 编译源码 2 1 Makefile文件 顶层目录 TOOL
  • 银行业法律法规与综合能力 第四章 银行从业法律基础 25%

    第四章 银行从业法律基础 4 1 银行基本法律法规 1 考点1 中国人民银行的职能和职责 一 职能 二 职责 考点2 中国人民银行的监督管理 一 直接检查监督杈 二 建议检查监督杈 三 特定情况下的检查监督权 考点3 国务院银行业监督管理机
  • hexo引用本地图片无法显示

    最近重新开始用起hexo 但是发现在文章中引用本地图片时总是显示不出来 问题如下图所示 花费了许久时间才解决这个问题 因此将一些解决经验整理出来 希望能帮助到大家 一 插件安装与配置 首先我们需要安装一个图片路径转换的插件 这个插件名字是h
  • 2023年智慧农业与经济发展国际研讨会议(ISSAED 2023)

    2023 International Seminar on Smart Agriculture and Economic Development 地点 合肥 智慧农业 农业信息管理系统 农业物联网系统集成与实践技术 农业大数据分析与应用 农
  • LLVM学习入门(2):实现解析器 Parser 和语法树 AST

    实现解析器 Parser 和语法树 AST 2 1 The Abstract Syntax Tree AST 语法抽象树 2 2 Parser Basics 基本的解析器 2 3 Basic Expression Parsing 基本表达式
  • 计算机与不确定性原理,不确定性原理

    题目 A simple baseline for bayesian uncertainty in deep learning 摘要 本文提出了一种简单 可扩展 通用的面向深度学习的不确定性表示和标定方法SWA Gaussian SWAG 随
  • SCILAB-自由科学计算软件

    SCILAB 自由科学计算软件 原创 2006 04 03 15 05 15 发表者 phoenixlin SCILAB是由法国国家信息与自动化研究院 INRIA 的科学家们开发的 开放源码 科学计算自由软件 SCILAB一词来源于英文 S
  • Graphics2D绘制图片,线段、矩形、圆形

    新建图片 BufferedImage newImage new BufferedImage 1079 512 BufferedImage TYPE INT RGB 获取绘图对象 Graphics2D g2d newImage createG
  • 访问云服务器文件共享,访问云服务器文件共享

    访问云服务器文件共享 内容精选 换一换 华为云帮助中心 为用户提供产品简介 价格说明 购买指南 用户指南 API参考 最佳实践 常见问题 视频帮助等技术文档 帮助您快速上手使用华为云服务 安装传输工具在本地主机和Windows云服务器上分别
  • 数组工具类

    该工具类有两个方法 1 isContained方法用来判断一个数组中是否包含另一个数组中所有的数据 2 arrayDiff方法用来删除一个数组中与另一个数组中值相同的元素 arrUtil js文件 key存在时表示是对象数据 可以不存在时表
  • Flying to the Mars(字典树)

    Flying to the Mars Time Limit 5000 1000 MS Java Others Memory Limit 32768 32768 K Java Others Total Submission s 12965 A