CSU 1130: Penney Game

举报
用户已注销 发表于 2021/11/19 04:37:43 2021/11/19
【摘要】 题目: Description Penney’s game is a simple game typically played by two players.  One version of the game calls for each player to choose a unique three-coi...

题目:

Description

Penney’s game is a simple game typically played by two players.  One version of the game calls for 
each player to choose a unique three-coin sequence such as HEADS TAILS HEADS (HTH).  A fair 
coin is tossed sequentially some number of times until one of the two sequences appears.  The 
player who chose the first sequence to appear wins the game. 
 
For this problem, you will write a program that implements a variation on the Penney Game.  You will 
read a sequence of 40 coin tosses and determine how many times each three-coin sequence 
appears.  Obviously there are eight such three-coin sequences: TTT, TTH, THT, THH, HTT, HTH, 
HHT and HHH.  Sequences may overlap.  For example, if all 40 coin tosses are heads, then the 
sequence HHH appears 38 times.

Input

The first line of input contains a single integer P, (1 £ P £ 1000), which is the number of data sets that 
follow.  Each data set consists of 2 lines.  The first line contains the data set number N.  The second 
line contains the sequence of 40 coin tosses.  Each toss is represented as an upper case H or an 
upper case T, for heads or tails, respectively.  There will be no spaces on any input line.

Output

For each data set there is one line of output.  It contains the data set number followed by a single 
space, followed by the number of occurrences of each three-coin sequence, in the order shown 
above, with a space between each one.  There should be a total of 9 space separated decimal 
integers on each output line.

Sample Input

4 
1 
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH 
2 
TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT 
3 
HHTTTHHTTTHTHHTHHTTHTTTHHHTHTTHTTHTTTHTH 
4 
HTHTHHHTHHHTHTHHHHTTTHTTTTTHHTTTTHTHHHHT

Sample Output

1 0 0 0 0 0 0 0 38
2 38 0 0 0 0 0 0 0
3 4 7 6 4 7 4 5 1
4 6 3 4 5 3 6 5 6


编程技巧:状态压缩

代码:


  
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int n, ans[8];
  6. cin >> n;
  7. char a, b, c;
  8. while (cin >> n)
  9. {
  10. cout << n;
  11. cin >> b >> c;
  12. for (int i = 0; i < 8; i++)ans[i] = 0;
  13. for (int i = 0; i < 38; i++)
  14. {
  15. a = b, b = c;
  16. cin >> c;
  17. ans[(a < 'T') * 4 + (b < 'T') * 2 + (c < 'T')]++;
  18. }
  19. for (int i = 0; i < 8; i++)cout << " " << ans[i];
  20. cout << endl;
  21. }
  22. return 0;
  23. }
  24. /**********************************************************************
  25. Problem: 1130
  26. User: 3901140225
  27. Language: C++
  28. Result: AC
  29. Time:4 ms
  30. Memory:2024 kb
  31. **********************************************************************/


文章来源: blog.csdn.net,作者:csuzhucong,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/nameofcsdn/article/details/79712440

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。