题解 SP7757 【MFLAR10 - Flowers Flourish from France】

原来SPOJ都一个测试点啊?

好吧,这题大家while啊?我用的是递归!

首先因为大小写都一样,所以我写了个全换成小写的函数:

char tounder(char x)
{
    if(x >= 'A' && x <= 'Z')
    {
        return x - 'A' + 'a';
    }
    else
    {
        return x;
    }
}

然后和一个递归读入函数:

void input()
{
    getline(cin, s);
    if(s == "*")
    {
        return ;
    }
    bool flag = false;
    s[0] = tounder(s[0]);
    for(int i = 1; i <= s.length() - 1; i++)
    {
        if(s[i] != ' ')
        {
            s[i] = tounder(s[i]);
        }
        if(s[i] == ' ')
        {
            s[i + 1] = tounder(s[i + 1]);
            if(s[i + 1] != s[0])
            {
                flag = true;
                break;
            }
        }
    }
    if(!flag)
    {
        cout << "Y\n";
    }
    else
    {
        cout << "N\n";
    }
    input();
}

其实相当于如果为空,那么下个字符肯定是首字符,用这个字符和s[0]对比就AC了!

完整代码:

#include <iostream>
#include <cstring>
using namespace std;

string s;

char tounder(char x)
{
    if(x >= 'A' && x <= 'Z')
    {
        return x - 'A' + 'a';
    }
    else
    {
        return x;
    }
}

void input()
{
    getline(cin, s);
    if(s == "*")
    {
        return ;
    }
    bool flag = false;
    s[0] = tounder(s[0]);
    for(int i = 1; i <= s.length() - 1; i++)
    {
        if(s[i] != ' ')
        {
            s[i] = tounder(s[i]);
        }
        if(s[i] == ' ')
        {
            s[i + 1] = tounder(s[i + 1]);
            if(s[i + 1] != s[0])
            {
                flag = true;
                break;
            }
        }
    }
    if(!flag)
    {
        cout << "Y\n";
    }
    else
    {
        cout << "N\n";
    }
    input();
}

int main()
{
    input();
    return 0;
}
posted @ 2020-10-05 19:22  HappyBobb  阅读(8)  评论(0)    收藏  举报  来源