首页 > ACM题库 > HDU-杭电 > HDU 1039 Easier Done Than Said?-字符串-[解题报告] C++
2013
11-26

HDU 1039 Easier Done Than Said?-字符串-[解题报告] C++

Easier Done Than Said?

问题描述 :

Password security is a tricky thing. Users prefer simple passwords that are easy to remember (like buddy), but such passwords are often insecure. Some sites use random computer-generated passwords (like xvtpzyo), but users have a hard time remembering them and sometimes leave them written on notes stuck to their computer. One potential solution is to generate “pronounceable” passwords that are relatively secure but still easy to remember.

FnordCom is developing such a password generator. You work in the quality control department, and it’s your job to test the generator and make sure that the passwords are acceptable. To be acceptable, a password must satisfy these three rules:

It must contain at least one vowel.

It cannot contain three consecutive vowels or three consecutive consonants.

It cannot contain two consecutive occurrences of the same letter, except for ‘ee’ or ‘oo’.

(For the purposes of this problem, the vowels are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’; all other letters are consonants.) Note that these rules are not perfect; there are many common/pronounceable words that are not acceptable.

输入:

The input consists of one or more potential passwords, one per line, followed by a line containing only the word ‘end’ that signals the end of the file. Each password is at least one and at most twenty letters long and consists only of lowercase letters.

输出:

For each password, output whether or not it is acceptable, using the precise format shown in the example.

样例输入:

a
tv
ptoui
bontres
zoggax
wiinq
eep
houctuh
end

样例输出:

<a> is acceptable.
<tv> is not acceptable.
<ptoui> is not acceptable.
<bontres> is not acceptable.
<zoggax> is not acceptable.
<wiinq> is not acceptable.
<eep> is acceptable.
<houctuh> is acceptable.

2011-12-20 16:28:14

地址:http://acm.hdu.edu.cn/showproblem.php?pid=1039

题意:判断一个字符串是否能被接受。要满足:1.含有元音字母。2.不存在3个连续的元音字母或辅音字母。 3.不存在连续两个相同的字母,除非是’ee’或’oo’。

代码:

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

char str[1010] ;

int isvowels (char ch)
{
    if (ch == 'a' || ch == 'e' ||
        ch == 'i' || ch == 'o' || ch == 'u') return 1 ;
    return 0 ;
}

int t1(char str[])
{
    int i ;
    for (i = 0 ;str[i]  ;i++)
        if (isvowels(str[i])) return 1 ;
    return 0 ;
}

int t2(char str[])
{
    int i ;
    for (i = 2 ; str[i] ; i++)
    {
        if (isvowels(str[i]) && isvowels(str[i-1]) && isvowels(str[i-2]))
            return 0 ;
        if (!isvowels(str[i]) && !isvowels(str[i-1]) && !isvowels(str[i-2]))
            return 0 ;
    }
    return 1 ;
}

int t3(char str[])
{
    int i ;
    for (i = 1 ; str[i] ; i++)
        if (str[i] == str[i-1] && str[i] != 'e'&&str[i] != 'o') return 0 ;
    return 1 ;
}

int test(char str[])
{
    if (t1(str) == 0) return 0 ;
    if (t2(str) == 0) return 0 ;
    if (t3(str) == 0) return 0 ;
    return 1 ;
}

int main ()
{
    while (gets(str))
    {
        if (strcmp(str, "end") == 0) break ;
        printf ("<%s> is %sacceptable.\n", str, test(str)?"":"not ") ;
    }
    return 0 ;
}


  1. 嗯 分析得很到位,确实用模板编程能让面试官对你的印象更好。在设置辅助栈的时候可以这样:push时,比较要push的elem和辅助栈的栈顶,elem<=min.top(),则min.push(elem).否则只要push(elem)就好。在pop的时候,比较stack.top()与min.top(),if(stack.top()<=min.top()),则{stack.pop();min.pop();},否则{stack.pop();}.

  2. #include <cstdio>
    #include <cstring>

    const int MAXSIZE=256;
    //char store[MAXSIZE];
    char str1[MAXSIZE];
    /*
    void init(char *store) {
    int i;
    store['A']=’V', store['B']=’W',store['C']=’X',store['D']=’Y',store['E']=’Z';
    for(i=’F';i<=’Z';++i) store =i-5;
    }
    */
    int main() {
    //freopen("input.txt","r",stdin);
    //init(store);
    char *p;
    while(fgets(str1,MAXSIZE,stdin) && strcmp(str1,"STARTn")==0) {
    if(p=fgets(str1,MAXSIZE,stdin)) {
    for(;*p;++p) {
    //*p=store[*p]
    if(*p<’A’ || *p>’Z') continue;
    if(*p>’E') *p=*p-5;
    else *p=*p+21;
    }
    printf("%s",str1);
    }
    fgets(str1,MAXSIZE,stdin);
    }
    return 0;
    }