HDU——1039 Easier Done Than Said?

本文介绍了一种检查密码是否符合特定安全标准的方法,包括至少包含一个元音字母、避免连续三个相同类型的字母以及允许某些重复字符等规则,并提供了一个C++实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
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.

Input
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.

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

Sample Input

a
tv
ptoui
bontres
zoggax
wiinq
eep
houctuh
end

Sample Output

<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.

C++代码:

#include<iostream>
#include<string.h>
#define maxsize 1000
using namespace std;
int fun1(char *pw,int length){
    int count=0;
    for(int i=0;i<length;i++){
        if(pw[i]=='a'||pw[i]=='e'||pw[i]=='i'||pw[i]=='o'||pw[i]=='u')
            count++;
    }
    if(count>0)
        return 1;
    else
        return 0;
}
int fun2(char *pw,int length){
    for(int i=0;i<=length-3;i++){
        if(pw[i]=='a'||pw[i]=='e'||pw[i]=='i'||pw[i]=='o'||pw[i]=='u'){
            if(pw[i+1]=='a'||pw[i+1]=='e'||pw[i+1]=='i'||pw[i+1]=='o'||pw[i+1]=='u')
                if(pw[i+2]=='a'||pw[i+2]=='e'||pw[i+2]=='i'||pw[i+2]=='o'||pw[i+2]=='u')
                    return 0;
        }
        else if(pw[i]!='a'&&pw[i]!='e'&&pw[i]!='i'&&pw[i]!='o'&&pw[i]!='u'){
            if(pw[i+1]!='a'&&pw[i+1]!='e'&&pw[i+1]!='i'&&pw[i+1]!='o'&&pw[i+1]!='u')
                if(pw[i+2]!='a'&&pw[i+2]!='e'&&pw[i+2]!='i'&&pw[i+2]!='o'&&pw[i+2]!='u')
                    return 0;
        }
        }
    return 1;
}
int fun3(char *pw,int length){
    for(int i=0;i<length-1;i++)
        if(pw[i]!='e'&&pw[i]!='o'&&pw[i+1]==pw[i])
            return 0;
    return 1;
}

int main(){
    char *pw;
    pw=new char[maxsize];
    while(cin>>pw){
        if(strcmp(pw,"end")==0)
            break;
        else{
            int length=strlen(pw);
            if(fun1(pw,length)&&fun2(pw,length)&&fun3(pw,length))
                cout<<"<"<<pw<<"> is acceptable."<<endl;
            else
                cout<<"<"<<pw<<"> is not acceptable."<<endl;
        }
    }
    return 0;
}
### HDU OJ Problem 2566 Coin Counting Solution Using Simple Enumeration and Generating Function Algorithm #### 使用简单枚举求解硬币计数问题 对于简单的枚举方法,可以通过遍历所有可能的组合方式来计算给定面额下的不同硬币组合数量。这种方法虽然直观但效率较低,在处理较大数值时性能不佳。 ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] coins = {1, 2, 5}; // 定义可用的硬币种类 while (scanner.hasNext()) { int targetAmount = scanner.nextInt(); int countWays = findNumberOfCombinations(targetAmount, coins); System.out.println(countWays); } } private static int findNumberOfCombinations(int amount, int[] denominations) { if (amount == 0) return 1; if (amount < 0 || denominations.length == 0) return 0; // 不使用当前面值的情况 int excludeCurrentDenomination = findNumberOfCombinations(amount, subArray(denominations)); // 使用当前面值的情况 int includeCurrentDenomination = findNumberOfCombinations(amount - denominations[0], denominations); return excludeCurrentDenomination + includeCurrentDenomination; } private static int[] subArray(int[] array) { if (array.length <= 1) return new int[]{}; return java.util.Arrays.copyOfRange(array, 1, array.length); } } ``` 此代码实现了通过递归来穷尽每一种可能性并累加结果的方式找到满足条件的不同组合数目[^2]。 #### 利用母函数解决硬币计数问题 根据定义,可以将离散序列中的每一个元素映射到幂级数的一个项上,并利用这些多项式的乘积表示不同的组合情况。具体来说: 设 \( f(x)=\sum_{i=0}^{+\infty}{a_i*x^i}\),其中\( a_i \)代表当总金额为 i 时能够组成的方案总数,则有如下表达式: \[f_1(x)=(1+x+x^2+...)\] 这实际上是一个几何级数,其封闭形式可写作: \[f_1(x)=\frac{1}{(1-x)}\] 同理,对于其他类型的硬币也存在类似的生成函数。因此整个系统的生成函数就是各个单独部分之积: \[F(x)=f_1(x)*f_2(x)...*f_n(x)\] 最终目标是从 F(x) 中提取系数即得到所需的结果。下面给出基于上述理论的具体实现: ```cpp #include<iostream> using namespace std; const int MAXN = 1e4 + 5; int dp[MAXN]; void solve() { memset(dp, 0, sizeof(dp)); dp[0] = 1; // 初始化基础状态 int values[] = {1, 2, 5}, size = 3; for (int j = 0; j < size; ++j){ for (int k = values[j]; k <= 10000; ++k){ dp[k] += dp[k-values[j]]; } } } int main(){ solve(); int T; cin >> T; while(T--){ int n; cin>>n; cout<<dp[n]<<endl; } return 0; } ``` 这段 C++ 程序展示了如何应用动态规划技巧以及生成函数的概念高效地解决问题实例[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值