hdu 3460 Ancient Printer

浅浅的花香味﹌ 2021-12-16 19:15 290阅读 0赞

http://acm.hdu.edu.cn/showproblem.php?pid=3460

  这题题意我就不想说了,做的真实气死我了。就是把给定数目的单词用他那个“ancient printer”给打印出来,让你把最少按键次数输出来。尼玛,

  我最初的想法很简单,就是在ac自动机的基础上去,用深搜来跑。因为要求最少嘛,所以记得当时自己就一直在想怎么能满足最优,先打印存在前后缀长度短的,当时YY了好久,但是总是感觉不行。后来实在没办法才去google的,发现他们的方法特别。。,其实我当时也想到了,但是偏执的想跑一下深搜,结果自己还玩大了。按键次数=单词个数(因为要按print键)+2*总共结点个数(ans)-最长字符串长度(ma)。最长放在最后打印,而且打印完之后可以允许字符依旧存在于打印机之中,不必退格删掉,所以要减去。

  代码:

ContractedBlock.gif ExpandedBlockStart.gif

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cstdio>
  6. using namespace std;
  7. const int maxn=26;
  8. struct node
  9. {
  10. node *next[maxn];
  11. node()
  12. {
  13. for(int i=0;i<maxn;i++)
  14. {
  15. if(next[i]==NULL) continue;
  16. next[i]=NULL;
  17. }
  18. }
  19. ~node()
  20. {
  21. for(int i=0;i<maxn;i++)
  22. {
  23. if(next[i]!=NULL)
  24. next[i]=NULL;
  25. }
  26. }
  27. };
  28. inline int max(int a,int b)
  29. {
  30. return a<b?b:a;
  31. }
  32. int ma,ans,n;
  33. class Trie
  34. {
  35. public:
  36. node *root;
  37. Trie()
  38. {
  39. root=NULL;
  40. }
  41. void insert(char *s)
  42. {
  43. if(!root)
  44. root=new node();
  45. int len=strlen(s);
  46. ma=max(ma,len);
  47. node *loca=root;
  48. for(int i=0;i<len;i++)
  49. {
  50. int id=s[i]-'a';
  51. if(loca->next[id]==NULL)
  52. {
  53. ans++;
  54. loca->next[id]=new node();
  55. }
  56. loca=loca->next[id];
  57. }
  58. }
  59. };
  60. int main()
  61. {
  62. char s[55];
  63. while(~scanf("%d",&n))
  64. {
  65. Trie t;
  66. ans=ma=0;
  67. for(int i=0;i<n;i++)
  68. {
  69. scanf("%s",s);
  70. t.insert(s);
  71. }
  72. printf("%d\n",n+2*ans-ma);
  73. }
  74. return 0;
  75. }

善待每一天,努力做好自己。

欢迎转载,注明出处。

转载于:https://www.cnblogs.com/RainingDays/archive/2013/05/09/3068524.html

发表评论

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

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

相关阅读

    相关 POJ2159 Ancient Cipher

    题目大意:字符串加密问题,采用字符替换和重新排列的综合方法加密。输入为加密串和字符串,不含空格的大写字母。 解题思路:两种加密方法在具体过程中应用并不唯一,即我们不