• [Swust OJ 795]--Penney Game


    题目链接:http://acm.swust.edu.cn/problem/795/

    Time limit(ms): 1000        Memory limit(kb): 65535
     
    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
     


    题目大意:可以理解为抛硬币游戏,HT代表正反面,这里选取3次结果为一组分别按照这个顺序TTT, TTH, THT, THH, HTT, HTH, HHT, HHH. 
         下面输入一个数n,代表测试数据的组数,然后每一组一个40个字符的字符串,输出上面对应的每一种排列的次数,输出的第一个数代表组号
     
    懂了意思都好说,直接上代码:
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <string>
     5 using namespace std;
     6 string x, s[] = { "TTT", "TTH", "THT", "THH", "HTT", "HTH", "HHT", "HHH" };
     7 int t, n, cnt[10];
     8 int main(){
     9     cin >> t;
    10     while (t--){
    11         memset(cnt, 0, sizeof(cnt));
    12         cin >> n >> x;
    13         for (int i = 0, len = x.size(); i < 8; i++){
    14             int pos = -1;
    15             while (1){
    16                 pos = x.find(s[i].c_str(), pos + 1);
    17                 if (pos == string::npos) break;//npos代表一个不存在的量
    18                 cnt[i]++;
    19             }
    20         }
    21         cout << n;
    22         for (int i = 0; i < 8; i++) cout << ' ' << cnt[i];
    23         cout << endl;
    24     }
    25     return 0;
    26 }
    View Code
  • 相关阅读:
    Spring Boot发布2.6.2、2.5.8:升级log4j2到2.17.0
    如何优雅地读写HttpServletRequest和HttpServletResponse的请求体
    更快的Maven来了
    Spring Cloud Gateway过滤器精确控制异常返回(实战,控制http返回码和message字段)
    NumPy学习笔记
    来自Java程序员的Python新手入门小结
    Java应用日志如何与Jaeger的trace关联
    Jaeger知识点补充
    分布式调用链跟踪工具Jaeger?两分钟极速体验
    Spring Cloud Gateway过滤器精确控制异常返回(实战,完全定制返回body)
  • 原文地址:https://www.cnblogs.com/zyxStar/p/4593260.html
Copyright © 2020-2023  润新知