湖南省第十一届程序设计大赛 (水题)聊天止于呵呵

川长思鸟来 2022-07-17 01:44 218阅读 0赞

20160825215711342

模拟题老师基本上都不讲,因为只要足够细心,大家都能做对。集训到今天就结束了。这次下来,

发现自己做模拟题已经很有手感了,相较于之前,真的有很大的成长。

简单的分析下这道题,

1、首先,要找出总共有多少段对话,就想到了用map来存对话是否出现过,A->B,B->A只要没有出现hehehe…作为结束就属于同一段对话。

2、处理对话内容,把非字母转成空格,大写转小写,然后要把对话拆成单词,看是否是由hehehe…组成,就想到了前段学的stringstream

3、确定每个单词是否是由hehehe组成,这个非常简单,

输入要稍稍注意下,因为对话有空格的所以只能用gets(),或getline()接收,(或者其它我没学的,不以空格结束的接收字符串的)

代码如下:

  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include<cstdio>
  5. #include<map>
  6. #include<cstring>
  7. #include <cmath>
  8. using namespace std;
  9. map<string, int> cnt;
  10. int chi(char c[])
  11. {
  12. int clen= strlen(c);
  13. if(clen%2!=0)
  14. return 0;
  15. else
  16. {
  17. for(int i=0; i<clen-1; i=i+2)
  18. {
  19. if(c[i]!='h'||c[i+1]!='e')
  20. return 0;
  21. }
  22. return 1;
  23. }
  24. }
  25. int check(char s[])
  26. {
  27. int slen=strlen(s);
  28. for(int i=0; i<slen; i++)
  29. {
  30. if(isalpha(s[i]))
  31. s[i] = tolower(s[i]);
  32. else
  33. s[i] = ' ';
  34. }
  35. stringstream ss(s);
  36. char c[1005];
  37. while(ss>>c)
  38. {
  39. if(chi(c)==1)
  40. return 1;
  41. }
  42. return 0;
  43. }
  44. int main()
  45. {
  46. char a, b, s[1005];
  47. double sum=0, cont=0;
  48. while((scanf("%c->%c:", &a, &b))!=EOF)
  49. {
  50. gets(s);
  51. string z, y;
  52. z += a+b;
  53. y += b+a;
  54. if((!cnt.count(z)&&!cnt.count(y))||(cnt[y]==0&&cnt[z]==0))
  55. {
  56. cnt[y]=1;
  57. cnt[z]=1;
  58. sum++;
  59. }
  60. if(check(s)==1) //s串中有hehehe;
  61. {
  62. cont++;
  63. cnt[z]=0;
  64. cnt[y]=0;
  65. }
  66. }
  67. printf("%.0lf%%\n", cont/sum*100.0);
  68. return 0;
  69. }

发表评论

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

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

相关阅读

    相关 公交车19路换新车啦,

    19路车是我每天上班必坐的,过去的(昨天为止)19路车 由于使用年限过久,造成车很容易就抛锚了。我们这些乘客只得下车等候后面的车,通常都是挤的不行。早就抱怨希望能换新车,今天竟