Flying to the Mars(字典树)

迷南。 2022-08-01 00:10 57阅读 0赞

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

1800-1.jpg
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

  1. 4
  2. 10
  3. 20
  4. 30
  5. 04
  6. 5
  7. 2
  8. 3
  9. 4
  10. 3
  11. 4

Sample Output

  1. 1
  2. 2

上篇那个用map过得太过偶然,因为数字太大(最多30位)整

数撑不下!当然了用map就可以过了,但是正好学

到字典树,用字典树试一下效果相当的好!得意大赞!

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<iostream>
  5. using namespace std;
  6. struct Tree
  7. {
  8. int num;
  9. Tree *next[10];
  10. Tree()
  11. {
  12. num=0;
  13. for(int i=0;i<10;i++)
  14. {
  15. next[i]=NULL;
  16. }
  17. }
  18. }*root;
  19. int insert(Tree *p,char *s)
  20. {
  21. int i=0;
  22. while(s[i])
  23. {
  24. int x=s[i]-'0';
  25. if(p->next[x]==NULL)
  26. {
  27. p->next[x]=new Tree();
  28. }
  29. p=p->next[x];
  30. i++;
  31. }
  32. p->num++;
  33. // cout<<"数为:"<<p->num<<endl;
  34. return (p->num);
  35. }
  36. int main()
  37. {
  38. char s[30],s1[30];
  39. int n,maxs,stl,i,len;
  40. while(cin>>n)
  41. {
  42. root=new Tree();
  43. maxs=-5;
  44. while(n--)
  45. {
  46. cin>>s;
  47. stl=strlen(s);
  48. i=len=0;
  49. for(i=0;i<stl;i++)
  50. {
  51. if(s[i]!='0')
  52. {
  53. break;
  54. }
  55. }
  56. for(i;i<stl;i++)
  57. {
  58. s1[len++]=s[i];
  59. }
  60. s1[len]='\0';
  61. // cout<<s1<<endl;
  62. maxs=max(maxs,insert(root,s1));
  63. }
  64. cout<<maxs<<endl;
  65. }
  66. }

num是在每一个已经装好的字符串后面记录一下,正好记录的是

这个字符串在文中的次数!

发表评论

表情:
评论列表 (有 0 条评论,57人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Fly

    转载至 [这位大佬][Link 1] Fly.js 是一个基于 promise 的,轻量且强大的Javascript http 网络库,它有如下特点: 提供统一的 Pr

    相关 字典

    Problem Description 遇到单词不认识怎么办? 查字典啊,已知字典中有n个单词,假设单词都是由小写字母组成。现有m个不认识的单词,询问这m个单词是否出现在

    相关 字典

    字典树是一种以树这种结构为基础建立的算法 ,那么字典树到底有哪些典型的应用呢? 1.字典树在串的快速检索中的应用。   给出N个单词组成的熟词表,以及一篇全用小写英文