当前位置:   article > 正文

usaco letter game

usaco letter game

这个代码是我转的,我自己写了好几次不是这里出错就是那里出错我实在受不了了。

  1. /*
  2. ID: jinbo wu
  3. LANG: C++
  4. TASK: lgame
  5. */
  6. #include<iostream>
  7. #include<istream>
  8. #include<stdio.h>
  9. #include<string.h>
  10. #include<math.h>
  11. #include<stack>
  12. #include<map>
  13. #include<algorithm>
  14. #include<queue>
  15. #define oo 1000000000
  16. #define ll long long
  17. #define pi (atan(2)+atan(0.5))*2
  18. using namespace std;
  19. struct node
  20. {
  21. int s[26];
  22. }h,p,q;
  23. int w[26]={2,5,4,4,1,6,5,5,1,7,6,3,5,2,3,5,7,2,1,2,4,6,6,7,5,7};
  24. int l,num,m,len[40000],M[40000],temp,ansnum,MaxM;
  25. char s[10],dict[40000][9],ans[501][10];
  26. void Find2(int z)
  27. {
  28. int i,j;
  29. for (i=z+1;i<=num;i++)
  30. if (M[i]<=m)
  31. {
  32. q=p;
  33. for (j=0;j<len[i];j++)
  34. if (!q.s[dict[i][j]-'a']) goto B;
  35. else q.s[dict[i][j]-'a']--;
  36. if (M[i]+M[temp]>MaxM)
  37. {
  38. MaxM=M[i]+M[temp];
  39. ansnum=1;
  40. strcpy(ans[1],dict[temp]);
  41. ans[1][len[temp]]=' ';
  42. strcpy(ans[1]+len[temp]+1,dict[i]);
  43. }else
  44. if (M[i]+M[temp]==MaxM)
  45. {
  46. ansnum++;
  47. strcpy(ans[ansnum],dict[temp]);
  48. ans[ansnum][len[temp]]=' ';
  49. strcpy(ans[ansnum]+len[temp]+1,dict[i]);
  50. }
  51. B: ;
  52. }
  53. }
  54. void getanswer()
  55. {
  56. int i,j;
  57. m=0;
  58. memset(h.s,0,sizeof(h.s));
  59. for (i=0;i<l;i++)
  60. {
  61. m+=w[s[i]-'a'];
  62. h.s[s[i]-'a']++;
  63. }
  64. ansnum=0; MaxM=0;
  65. for (i=1;i<=num;i++)
  66. if (len[i]<=l && M[i]<=m)
  67. {
  68. p=h;
  69. for (j=0;j<len[i];j++)
  70. if (!p.s[dict[i][j]-'a']) goto A;
  71. else p.s[dict[i][j]-'a']--;
  72. if (M[i]>MaxM)
  73. {
  74. MaxM=M[i];
  75. ansnum=1;
  76. strcpy(ans[1],dict[i]);
  77. }else
  78. if (M[i]==MaxM)
  79. strcpy(ans[++ansnum],dict[i]);
  80. if (l-len[i]>=3)
  81. {
  82. temp=i;
  83. m-=M[i];
  84. Find2(i);
  85. m+=M[i];
  86. }
  87. A: ;
  88. }
  89. return;
  90. }
  91. int main()
  92. {
  93. int i;
  94. freopen("lgame.dict","r",stdin);
  95. num=0;
  96. do
  97. {
  98. scanf("%s",dict[++num]);
  99. }while (dict[num][0]!='.');
  100. num--;
  101. for (i=1;i<=num;i++)
  102. {
  103. len[i]=strlen(dict[i]); M[i]=0;
  104. for (m=0;m<len[i];m++)
  105. M[i]+=w[dict[i][m]-'a'];
  106. }
  107. fclose(stdin);
  108. freopen("lgame.in","r",stdin);
  109. freopen("lgame.out","w",stdout);
  110. scanf("%s",s); l=strlen(s);
  111. getanswer();
  112. printf("%d\n",MaxM);
  113. for (i=1;i<=ansnum;i++) printf("%s\n",ans[i]);
  114. return 0;
  115. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/710472
推荐阅读
相关标签
  

闽ICP备14008679号