首页 > ACM题库 > HDU-杭电 > hdu 2733 Netiquette-字符串[解题报告]C++
2014
02-14

hdu 2733 Netiquette-字符串[解题报告]C++

Netiquette

问题描述 :

Mr. Manners teaches netiquette (“net etiquette”, particularly as it applies to email) at the local community college. There are many different aspects to proper netiquette, including courtesy, correct spelling, and correct grammar. Through experience Mr. Manners has found that his college’s email system does a good job of catching most spelling and grammatical errors, and he’s also found that most of his students are courteous. So there are four violations of netiquette that Mr. Manners pays careful attention to, and he’s devised a quick way to test for them. A message is suspicious if it contains any of the following:
1. two adjacent uppercase letters,
(because you might be SHOUTING)
2. a digit adjacent to a letter,
(because you might be l33t, d00d)
3. an isolated character other than a, A, or I,
(because u r probably abbreviating words; the spell checker doesn’t catch this for some reason)
4. two adjacent punctuation marks, unless one of them is a double quote (the character “).
(because you might be using an emoticon :-)
For this problem, all characters in an email message are printable ASCII characters with codes in the range 32..126 (inclusive). A punctuation mark is any character other than a letter, digit, or space. Two characters are adjacent if they are right next to each other, with no characters in between. An isolated character is one whose only adjacent characters (if any) are spaces. Your job is to write a program that can tell if a one-line email message is suspicious.

输入:

The input consists of one or more email messages, followed by a line containing only # that signals the end of the input. Each message is on a line by itself, does not begin or end with a space, and does not contain consecutive spaces. End-of-line characters occur at the end of every line (of course), but they are not considered to be part of the message. A message will contain 1..80 characters.

输出:

The input consists of one or more email messages, followed by a line containing only # that signals the end of the input. Each message is on a line by itself, does not begin or end with a space, and does not contain consecutive spaces. End-of-line characters occur at the end of every line (of course), but they are not considered to be part of the message. A message will contain 1..80 characters.

样例输入:

"This is a safe message," said 50 wise men.
DON'T b l8 for the Apple ][ user's group meeting.
I ate at "Al's Big Burritos" for lunch!
It's not OK to burp at your grandmother.
*BuT* YoU _CaN_ Do ThIs, YoU KnOw.
We 8 eight oranges.
#

样例输出:

OK
suspicious
OK
suspicious
OK
suspicious

好恶心的字符串题目。

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

char s[81];
int l;
int Dig(char c)
{
    if (c >= '0' && c <= '9')
    {
        return 1;
    }
    return 0;
}

int Upper(char c)
{
    if (c >= 'A' && c <= 'Z')
    {
        return 1;
    }
    return 0;
}

int Lower(char c)
{
    if (c >= 'a' && c <= 'z')
    {
        return 1;
    }
    return 0;
}

int Char(char c)
{
    if (Upper(c) || Lower(c))
    {
        return 1;
    }
    return 0;
}

int Punc(char c)
{
    if (Dig(c) || Char(c) || c == ' ')
    {
        return 0;
    }
    return 1;
}

int Iso(char c)
{
    if (c == 'A' || c == 'a' || c == 'I')
    {
        return 1;
    }
    return 0;
}

int check1()
{
    int i;
    for (i = 0; i < l - 1; i++)
    {
        if(Upper(s[i]) && Upper(s[i + 1]))
            return 0;
    }
    return 1;
}

int check2()
{
    int i;
    for (i = 0; i < l - 1; i++)
    {
        if ((Char(s[i]) && Dig(s[i + 1])) || (Char(s[i + 1]) && Dig(s[i])))
            return 0;
    }
    return 1;
}

int check3()
{
    int i;
    if (l == 1)
    {
        if (Iso(s[0]))
        {
            return 1;
        }
        return 0;
    }
    if (s[1] == ' '&& Iso(s[0]) == 0 && s[0] != ' ')
        return 0;
    if (s[l - 2] == ' ' && Iso(s[l - 1]) == 0 && s[l - 1] != ' ')
        return 0;
    
    for (i = 1; i < l - 1; i++)
    {
        if(s[i] != ' ' && s[i - 1] == ' ' && s[i + 1] == ' ' && Iso(s[i]) == 0)
            return 0;
    }
    return 1;
}

int check4()
{
    int i;
    
    for (i = 0; i < l - 1; i++)
    {
        if(Punc(s[i]) && Punc(s[i + 1]) && s[i] != '\"' && s[i + 1] != '\"')
        return 0;
    }
    return 1;
}

int check()
{
    if (check1() == 0)
    {
        return 0;
    }
    if (check2() == 0)
    {
        return 0;
    }
    if (check3() == 0)
    {
        return 0;
    }
    if (check4() == 0)
    {
        return 0;
    }
    return 1;    
}

int main()
{
    while (1)
    {
        gets(s);
        if (strcmp(s, "#") == 0)
        {
            break;
        }
        l = strlen(s);
        if (check())
        {
            printf("OK\n");
        }
        else
        {
            printf("suspicious\n");
        }
    }    
}

 

ACM之家原创,转载请注明出处:http://www.acmerblog.com/hdu-2733-netiquette-4331.html


  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  2. /*
    * =====================================================================================
    *
    * Filename: 1366.cc
    *
    * Description:
    *
    * Version: 1.0
    * Created: 2014年01月06日 14时52分14秒
    * Revision: none
    * Compiler: gcc
    *
    * Author: Wenxian Ni (Hello World~), [email protected]
    * Organization: AMS/ICT
    *
    * =====================================================================================
    */

    #include
    #include

    using namespace std;

    int main()
    {
    stack st;
    int n,i,j;
    int test;
    int a[100001];
    int b[100001];
    while(cin>>n)
    {
    for(i=1;i>a[i];
    for(i=1;i>b[i];
    //st.clear();
    while(!st.empty())
    st.pop();
    i = 1;
    j = 1;

    while(in)
    break;
    }
    while(!st.empty()&&st.top()==b[j])
    {
    st.pop();
    j++;
    }
    }
    if(st.empty())
    cout<<"YES"<<endl;
    else
    cout<<"NO"<<endl;
    }
    return 0;
    }

  3. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。

  4. 第二块代码if(it != mp.end())应改为if(it != mp.end() && (i+1)!=(it->second +1));因为第二种解法如果数组有重复元素 就不正确

  5. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。