HDU-1606-Excuses, Excuses!(字符串)

这道题其实不难,但是花了很长时间才做出来,就是因为少了getchar(),做字符串类型的题的时候一定要注意,若在输入字符串之前,前面的输入会有换行,则一定要用getchar()把回车吃了。

#include <iostream>
#include <string>
#include <algorithm>
#include <stdio.h>

using namespace std;

string list[21];
string str[21];
int ans[21];

int main()
{
    int n, m;
    int count = 1;
    while(~scanf("%d%d", &n, &m))
    {
        for(int i = 0; i < n; i++)
            cin >> list[i];
        
        getchar();    //吃掉输入单词后的回车 
        for(int i = 0; i < m; i++)
        {
            getline(cin, str[i]);
            ans[i] = 0;
        }
        
        string tmp;
        for(int i = 0; i < m; i++)
            for(int j = 0; j < str[i].length(); j++)
            {
                if(str[i][j] >= 'A' && str[i][j] <= 'Z')
                {
                    char ss = str[i][j] - 'A' + 'a';     //将大写字母转化为小写 
                    tmp = tmp + ss;    
                }
                else if(str[i][j] >= 'a' && str[i][j] <= 'z')
                    tmp = tmp + str[i][j];
                else
                {
                    for(int k = 0; k < n; k++)
                        if(list[k] == tmp)
                            ans[i]++;
                    tmp.clear();
                }
            }
                
        int max = 0;
        for(int i = 0; i < m; i++)
            if(max < ans[i])
                max = ans[i];
        
        cout << "Excuse Set #" << count << endl; 
        for(int i = 0; i < m; i++)
            if(ans[i] == max)
                cout << str[i] << endl;
        count++;
        cout << endl;
    }
    return 0;

猜你喜欢

转载自blog.csdn.net/mch2869253130/article/details/82083433
今日推荐