【冀宝要逆袭】UESTC-486-Good Morning

来源:互联网 发布:图书馆管理系统数据库 编辑:程序博客网 时间:2024/05/29 08:31

又是一道简单但是有点坑的题

问题描述

Sam loves Lily very much that he shows his love to her through all kinds of ways. This morning, Lily received an e-mail from Sam. Lily knows that Sam hided “good morning” in this mail. Lily tried several ways to resort the letters (including the space ’ ‘) so that more “good morning”s could be found. The number of “good morning” appeared in a specified string equals the number of positions from which Lily could see a consecutive string “good morning”.

With so many letters, Lily is about to be dizzy. She asks you to tell her what is the maximum number of “good morning”s appear in this mail after rearranged in some way.

输入

First an integer T (T≤20), indicates there are T test cases.

Every test case begins with a single line consist of only lowercase letters and space which is at most 1000 characters.

输出

For every test case, you should output Case #k: first, where k indicates the case number and starts at 1. Then output an integer indicating the answer to this test case.

样例输入

[“2\ngninrom doog\nggoooodd mmoorrnniinngg”]

样例输出

[“Case #1: 1\nCase #2: 2”]

#include <iostream>#include <string.h>#include <algorithm>using namespace std;int main(){    int t,i;    int cas=0;    string str;    int num[10];    cin>>t;    getline(cin,str);    while(t--)    {        memset(num,0,sizeof(num));        getline(cin,str);        for(i=0;i<str.size();i++)        {            if(str[i]=='g')num[0]++;            else if(str[i]=='o')num[1]++;            else if(str[i]=='d')num[2]++;            else if(str[i]=='m')num[3]++;            else if(str[i]=='n')num[4]++;            else if(str[i]=='r')num[5]++;            else if(str[i]=='i')num[6]++;            else if(str[i]==' ')num[7]++;        }        num[0]--;num[1]/=3;num[4]/=2;        int count=*min_element(num,num+7) ;        cout<<"Case #"<<++cas<<": "<<count<<endl;    }    return 0;}

这道题呢,刚开始的想法很简单就是统计每个字符的数目,然后除它的权重,统计每个字符的最小值就是hide的good morning的个数,但是之后即发现了一个问题,因为good morning的首字母和末尾字母都是g 如果出现了good morningood morning 这个情况的话是包含两个早上好的,但是结果只会输出1,考虑到以上连接情况发现g的值只要减一,即可给出答案。

0 0
原创粉丝点击