2368. Penney Game

            

2368. Penney Game

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

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, (1P1000), 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

HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH 

TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT 

HHTTTHHTTTHTHHTHHTTHTTTHHHTHTTHTTHTTTHTH 

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

Problem Source

每周一赛第一场

// Problem#: 2368

// Submission#: 1983741

// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/

// All Copyright reserved by Informatic Lab of Sun Yat-sen University

#include <iostream>

#include <cstring>

using namespace std;

class String

{

    char *s;

    int size;

public:

    String(){

        size=0;

        s = new char[size+1];

        s[0]='\0';

    }//default constructor, create an empty string, e.g. *s = ""

    ~String(){

        delete [] s;

    }   //destructor, destroy object

    String(const char str[]){

        size=strlen(str);

        s = new char[size+1];

        s[0]='\0';

        strcpy(s,str);

    }//parameterized constructor

    String (const String & str){

        size=str.size;

        s = new char[size+1];

        s[0]='\0';

        strcpy(s,str.s);

    }//copy constructor

    String(int num, const char c){

        size=num;

        int i;

        s = new char[size+1];

        for(i=0;i<size;i++){

            s[i]=c;

        }

        s[size]='\0';

    }//copy char c num times

    String& concate(const String str);//concatenate two strings, append str to current string

    int strLength(); //return size of string

    //Copies the portion of str that begins at the character position begin and spans len characters

    //or until the end of str, if either str is too short or if len is -1

    String (const String& str, int begin, int len= -1){

        size=strlen(str.s)-begin;

        if(len!=-1&&len<size){

            size=len;

        }

        s = new char[size+1];

        int i=0;

        while(i<size){

            s[i]=str.s[begin+i];

            i++;

        }

        s[i]='\0';

    }

    //Searches the string for the first occurrence of str

    //When pos is specified, the search only includes characters at or after position pos

    //ignoring any possible occurrences that include characters before pos.

    int find(const char str[], int pos,int l);

    //operand overloading, already defined for you

    friend ostream& operator << (ostream &cout, String str)

    {   

        cout<<str.s;

        return (cout);

    }

};

String& String:: concate(const String str){

    size+=str.size;

    strcat(s,str.s);

    return *this;

}

int String:: strLength(){

    return strlen(s);

}

int  String:: find(const char str[], int pos = 0,int l=3){

        if(str[0]==s[pos] && str[1]==s[pos+1] &&str[2]==s[pos+2] ){

            return 1;

        }

        else{

            return -1;

        }

};  

int main(){

    int n;

    cin>>n;

    int i;

    const char b[8][4]={"TTT","TTH","THT","THH","HTT","HTH","HHT","HHH"};

    for(i=1;i<=n;i++){

        int d;

        cin>>d;

        char *p=new char[41];

        cin>>p;

        String a(p);

        int c[8]={0};

        int x;

        for(x=0;x<39;x++){

            int y;

            for(y=0;y<8;y++){

                if(a.find(b[y],x,3)!=(-1)){

                    c[y]++;

                }

            }

        }

        cout<<i;

        for(x=0;x<8;x++){

            cout<<" "<<c[x];

        }

        cout<<endl;

    }

    return 0;

}                              

前面很风骚的把昨天作业要求编写的String类给用上了,其实根本就不用那么长。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值