hdu---(1800)Flying to the Mars(trie树)

Flying to the Mars

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

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

Author

PPF@JLU

由于数据疲弱,所以其实使用map就可以了,虽然这道题有人为的想卡掉STL做法,但是还存在漏洞。

代码:

 1 //#define LOCAL
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<map>
 6 using namespace std;
 7 map<int,int>aa;
 8 int main()
 9 {
10  int n,i,maxc,b;
11  #ifdef LOCAL
12   freopen("test.in","r",stdin);
13  #endif
14  while(scanf("%d",&n)!=EOF){
15     aa.clear();
16       maxc=1;
17     for(i=1;i<=n;i++){
18       scanf("%d",&b);
19       aa[b]++;
20      if(maxc<aa[b])maxc=aa[b];
21     }
22     printf("%d\n",maxc);
23  }
24   return 0;
25 }

不过,还是为了练一下手,就是用比较简单的trie树吧!

 代码:218ms

 1 //#define LOCAL
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 typedef struct node
 6 {
 7   struct node *child[10];
 8   int id;
 9 }Trie;
10 int max(int a,int b)
11 {
12   return a>b?a:b;
13 }
14 int Insert(char *s,Trie *root)
15 {
16     Trie *cur=root,*curnew;
17     int i,pos;
18     for(i=0;s[i]!='\0';i++)
19     {
20         pos=s[i]-'0';
21       if(cur->child[pos]==NULL)
22       {
23            curnew = new Trie;
24          curnew->id=0;
25          for(int j=0;j<10;j++)
26            curnew->child[j]=NULL;
27          cur->child[pos]=curnew;
28       }
29       cur=cur->child[pos];
30     }
31      cur->id++;
32   return cur->id;
33 }
34 void del(Trie *root)
35 {
36     Trie *cur=root;
37     for(int i=0;i<10;i++)
38     if(cur->child[i]!=NULL)
39        del(cur->child[i]);
40    delete cur;
41 }
42 char bb[32];
43 int main()
44 {
45     int n,ans,i;
46     #ifdef LOCAL
47     freopen("test.in","r",stdin);
48     #endif
49     Trie *root;
50     while(scanf("%d",&n)!=EOF)
51     {
52       root=new Trie;
53      for(i=0;i<10;i++)
54        root->child[i]=NULL;
55         ans=1;
56       while(n--){
57         scanf("%s",bb);
58         i=0;
59         while(bb[i]=='0'&&bb[i+1]!='\0')i++;
60         ans=max(ans,Insert(bb+i,root));
61       }
62       printf("%d\n",ans);
63       del(root);
64     }
65  return 0;
66 }

注意几组数据:

  4 004 04 0012 000

  3 00 0 000

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏ml

HDUOJ---hello Kiki

Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K...

2819
来自专栏一个会写诗的程序员的博客

Caused by: android.os.NetworkOnMainThreadException

Caused by: android.os.NetworkOnMainThreadException at android.os.StrictMode$And...

1472
来自专栏别先生

Caused by: java.net.ConnectException: Connection refused: master/192.168.3.129:7077

1:启动Spark Shell,spark-shell是Spark自带的交互式Shell程序,方便用户进行交互式编程,用户可以在该命令行下用scala编写spa...

8796
来自专栏HansBug's Lab

3314: [Usaco2013 Nov]Crowded Cows

3314: [Usaco2013 Nov]Crowded Cows Time Limit: 1 Sec  Memory Limit: 128 MB Submit...

2675
来自专栏ml

hdu---(2604)Queuing(矩阵快速幂)

Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (...

41511
来自专栏Golang语言社区

Knapsack problem algorithms for my real-life carry-on knapsack

I'm a nomad and live out of one carry-on bag. This means that the total weight o...

1552
来自专栏c#开发者

How to pass multiple values from child widow

In my previous post which was relating to implementing dialog box in web based a...

3688
来自专栏醉梦轩

Linux上C语言写的简易telnet客户端

1842
来自专栏ml

HDU----(4291)A Short problem(快速矩阵幂)

A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32...

3516
来自专栏ml

HDUOJ-----1085Holding Bin-Laden Captive!

Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limi...

27911

扫码关注云+社区

领取腾讯云代金券