B. All the Vowels Please

博客围绕Codeforces上的一道题目展开,题目要求根据给定长度k构造一个单词,该单词按n行m列排列时,每行每列都至少包含一次英语元音字母。若存在则输出单词,不存在则输出 -1。解题思路是找出5*5的图表后按顺序循环输出。

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

题目链接:https://codeforces.com/contest/1166/problem/B

B. All the Vowels Please

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Tom loves vowels, and he likes long words with many vowels. His favorite words are vowelly words. We say a word of length kk is vowelly if there are positive integers nn and mm such that nm=knm=k and when the word is written by using nn rows and mm columns (the first row is filled first, then the second and so on, with each row filled from left to right), every vowel of the English alphabet appears at least once in every row and every column.

You are given an integer kk and you must either print a vowelly word of length kk or print −1−1 if no such word exists.

In this problem the vowels of the English alphabet are 'a', 'e', 'i', 'o' ,'u'.

Input

Input consists of a single line containing the integer kk (1≤k≤1041≤k≤104) — the required length.

Output

The output must consist of a single line, consisting of a vowelly word of length kk consisting of lowercase English letters if it exists or −1−1 if it does not.

If there are multiple possible words, you may output any of them.

Examples

input

Copy

7

output

Copy

-1

input

Copy

36

output

Copy

agoeuioaeiruuimaeoieauoweouoiaouimae

Note

In the second example, the word "agoeuioaeiruuimaeoieauoweouoiaouimae" can be arranged into the following 6×66×6 grid:

https://i-blog.csdnimg.cn/blog_migrate/5593bf70a9ee312d97947b95dfcbb208.png

It is easy to verify that every row and every column contain all the vowels.

 

思路:最最重要的,找出5*5的图表,之后就按顺序循环输出即可。

 

a

e

i

o

u

a

e

i

o

u

a

e

i

o

u

a

e

i

o

u

a

e

i

o

u

a

e

i

o

u

a

e

i

o

u

a

 

代码:

#include<iostream>

#include<algorithm>

#include<string>

#include<cmath>

using namespace std;

int main()

{

         int fla = 0;

         int m, n;

         int k;

         cin >> k;

         for (int i = 5; i <= sqrt(k); i++)

         {

                  if (k%i == 0)

                  {

                          m = i;

                          n = k / i;

                          fla = 1;

                          break;

                  }

         }

         if (fla == 0)cout << -1;

         else

         {

                  for (int i = 1; i <= m; i++)

                  {

                          if (i % 5 == 1)

                          {

                                   for (int j = 1; j <= n; j++)

                                   {

                                            if (j % 5 == 1)cout << "a";

                                            if (j % 5 == 2)cout << "e";

                                            if (j % 5 == 3)cout << "i";

                                            if (j % 5 == 4)cout << "o";

                                            if (j % 5 == 0)cout << "u";

                                   }

                          }

                          if (i % 5 == 2)

                          {

                                   for (int j = 1; j <= n; j++)

                                   {

                                            if (j % 5 == 1)cout << "e";

                                            if (j % 5 == 2)cout << "i";

                                            if (j % 5 == 3)cout << "o";

                                            if (j % 5 == 4)cout << "u";

                                            if (j % 5 == 0)cout << "a";

                                   }

                          }

                          if (i % 5 == 3)

                          {

                                   for (int j = 1; j <= n; j++)

                                   {

                                            if (j % 5 == 1)cout << "i";

                                            if (j % 5 == 2)cout << "o";

                                            if (j % 5 == 3)cout << "u";

                                            if (j % 5 == 4)cout << "a";

                                            if (j % 5 == 0)cout << "e";

                                   }

                          }

                          if (i % 5 == 4)

                          {

                                   for (int j = 1; j <= n; j++)

                                   {

                                            if (j % 5 == 1)cout << "o";

                                            if (j % 5 == 2)cout << "u";

                                            if (j % 5 == 3)cout << "a";

                                            if (j % 5 == 4)cout << "e";

                                            if (j % 5 == 0)cout << "i";

                                   }

                          }

                          if (i % 5 == 0)

                          {

                                   for (int j = 1; j <= n; j++)

                                   {

                                            if (j % 5 == 1)cout << "u";

                                            if (j % 5 == 2)cout << "a";

                                            if (j % 5 == 3)cout << "e";

                                            if (j % 5 == 4)cout << "i";

                                            if (j % 5 == 0)cout << "o";

                                   }

                          }

                  }

         }

}

 

<think>好的,我现在要解决用户的问题:用Python的map、reduce和filter处理句子,完成四个任务。首先,我需要明确每个任务的具体要求,然后考虑如何结合这三个函数来实现。 首先,用户提到的四个操作分别是: 1. 筛选出以元音字母开头的单词。 2. 找出包含标点在内的最短单词。 3. 把所有单词转成大写字母形式。 4. 删除句子里的所有元音字符。 我需要分步骤处理每个任务,并思考如何用map、reduce和filter来实现。 第一个任务:筛选以元音开头的单词。元音字母包括A、E、I、O、U,不区分大小写。这里应该用filter函数。需要检查每个单词的首字母是否在元音集合中。需要注意的是,可能要考虑单词是否为空的情况,但通常split后的单词不会有这种情况。比如,用lambda函数判断word[0].lower()是否在{'a','e','i','o','u'}中。 第二个任务:找包含标点的最短单词。最短可能要考虑多个单词长度相同的情况,但用户只要找到其中一个。这里可以用reduce函数,比较每个单词的长度,保留较短的。初始值可以设为第一个单词,然后逐个比较。需要注意的是,split后的列表可能为空,需要处理异常情况,但假设用户输入的句子是有效的。 第三个任务:转大写。这明显适合用map函数,对每个单词应用upper()方法。 第四个任务:删除所有元音字符。这里可能需要先处理整个句子,或者每个单词中的字符。用户要求的是从句子中移除所有元音,所以可能需要先将句子中的每个字符过滤掉元音。但用户给出的示例引用中有处理字符串的例子,比如移除空格,所以可能需要用map或列表推导式来处理每个字符,然后join。但用户要求用map、reduce、filter,所以可能需要结合使用。 接下来,整合这些步骤。可能需要将句子分割成单词,处理各个步骤,注意顺序。例如,先筛选元音开头的单词,再转大写,处理删除元音可能需要在整个字符串上进行,或者每个单词处理后再合并。 不过,根据用户的问题,可能每个任务是独立的,即四个不同的处理,而不是连续的步骤。比如,用户可能需要分别实现这四个功能,而不是在一个流程中完成。所以可能需要分别写四个不同的代码段。 比如,对于第一个任务: sentence = "Hello, this is an example sentence! Let's process it." words = sentence.split() 以元音开头的单词: vowel_start = list(filter(lambda word: word[0].lower() in {'a','e','i','o','u'}, words)) 第二个任务,最短单词: from functools import reduce shortest = reduce(lambda x, y: x if len(x) <= len(y) else y, words) 第三个任务,转大写: upper_words = list(map(lambda word: word.upper(), words)) 第四个任务,删除所有元音字符。这里需要将整个句子中的元音去掉,不管大小写。可以用map处理每个字符,然后filter掉元音: vowels = {'a','e','i','o','u','A','E','I','O','U'} filtered_chars = filter(lambda c: c.upper() not in vowels, sentence) result = ''.join(filtered_chars) 或者,用map的话可能不太直接,因为每个字符需要判断是否保留,所以更适合filter。例如,使用filter来排除元音字符。 现在,用户要求必须使用map、reduce、filter,可能需要强制使用这三个函数。但有些情况下,可能某个任务更适合其中一种函数。例如,删除元音用filter,转大写用map,找最短用reduce,筛选元音开头用filter。 那么,将这些组合起来: 针对每个任务: 1. 筛选元音开头:使用filter,判断单词首字母是否在元音集合中。 2. 找最短单词:用reduce,比较每个单词的长度,保留较短的。 3. 转大写:用map对每个单词应用upper()。 4. 删除句子中的元音:将整个句子视为字符列表,用filter去除元音字符,然后用join组合。 现在,检查是否符合用户的要求。比如,如何处理标点符号?split()默认会根据空格分割,但可能包含标点的单词,比如"example,"。split()会保留单词后面的标点,所以在处理最短单词时,标点会被包括进去,这符合用户的要求。 例如,单词"is"长度为2,而"a."长度也是2,如果有这样的单词,reduce会找到第一个出现的最短单词吗?是的,因为当长度相同时,reduce会保留前一个,因为x的长度等于y的话,返回x。 对于第四个任务,删除所有元音字符,包括大写和小写的。可以用filter过滤每个字符,只要字符不在元音集合中。例如: no_vowels = ''.join(filter(lambda c: c.upper() not in {'A','E','I','O','U'}, sentence)) 现在,把这些写成代码: 假设输入的句子是sentence,首先split成单词列表: import re from functools import reduce sentence = "Hello, this is an example! A test sentence with some words." # 分割单词,可能包含标点 words = sentence.split() # 1. 筛选以元音开头的单词 vowel_start_words = list(filter(lambda w: w[0].lower() in {'a','e','i','o','u'}, words)) # 2. 找最短单词(包含标点) shortest_word = reduce(lambda x, y: x if len(x) <= len(y) else y, words) if words else None # 3. 转大写 upper_case_words = list(map(str.upper, words)) # 4. 删除所有元音字符 def remove_vowels(char): return char.upper() not in {'A','E','I','O','U'} no_vowels_sentence = ''.join(filter(remove_vowels, sentence)) 然后输出结果。需要注意,split()可能无法正确处理所有标点情况,例如连在一起的标点,但根据用户提供的引用[1],使用split(' ')按空格分割,所以这里假设单词是由空格分隔的,标点保留在单词中。 另外,对于问题中的例子,用户可能希望将这四个操作分别展示,而不是连续处理。所以每个操作都是独立的。 现在,是否符合用户的代码要求?是的,使用了map、reduce、filter三个函数。 然后,用户要求生成相关问题,可能涉及Python函数式编程、字符串处理、PEP8规范等。 总结代码: 可能需要导入reduce,因为在Python3中reduce在functools模块中。 现在,测试这些代码: 例如,输入句子:"Hello, this is an example! A test sentence with some words." 筛选以元音开头的单词:单词首字母为H(不是),t(不是),i(是),e(是),A(是),t,s,w,s,w。所以符合条件的单词是"is", "example!", "A"。 注意,原句中的单词分割后可能是["Hello,", "this", "is", "an", "example!", "A", "test", "sentence", "with", "some", "words."]。那么: "Hello,"首字母H,不是元音。 "this"首字母t,不是。 "is"首字母i,是。 "an"首字母a,是。 "example!"首字母e,是。 "A"首字母A,是。 其他单词首字母不符合。所以vowel_start_words应该是["is", "an", "example!", "A"] 但原句中的分割是否正确?例如,"an"是其中一个单词,正确。 最短单词:比如"A"长度为1,但原句中是否有更短的?例如,"A"是其中一个单词,长度是1(假设split后的单词是"A")。而其他单词如"is"长度为2,"an"长度2,"example!"长度8等。所以最短是"A"。 转大写后的单词都是大写形式。 删除元音后的句子,例如原句中的"Hello,"变成"Hll,",移除了'e'和'o'。 这样,代码应该能正确执行。 最后,确保代码符合PEP8规范,比如函数名用小写,空格等。例如,在引用[1]中提到split和join的使用,以及PEP8规范,可能需要注意代码的格式。</think>以下是使用Python的`map`、`reduce`和`filter`实现句子处理的代码及分步说明: --- ### 1. 筛选以元音开头的单词 ```python words = sentence.split() vowel_start = list(filter(lambda w: w[0].lower() in {'a','e','i','o','u'}, words)) ``` - **说明**:用`filter`筛选首字母为元音(不区分大小写)的单词[^1]。 --- ### 2. 找出包含标点的最短单词 ```python from functools import reduce shortest = reduce(lambda x, y: x if len(x) <= len(y) else y, words) if words else None ``` - **说明**:用`reduce`比较单词长度,保留最短的(若多个最短,返回第一个)[^2]。 --- ### 3. 所有单词转大写 ```python upper_case = list(map(str.upper, words)) ``` - **说明**:用`map`对每个单词应用`upper()`方法转换为大写。 --- ### 4. 移除句子中的所有元音字符 ```python no_vowels = ''.join(filter(lambda c: c.upper() not in {'A','E','I','O','U'}, sentence)) ``` - **说明**:用`filter`过滤掉所有元音字符(无论大小写),再拼接为字符串[^3]。 --- ### 完整代码示例 ```python from functools import reduce sentence = "Hello, this is an example! A test sentence with some words." words = sentence.split() # 1. 筛选以元音开头的单词 vowel_start = list(filter(lambda w: w[0].lower() in {'a','e','i','o','u'}, words)) # 2. 找最短单词 shortest = reduce(lambda x, y: x if len(x) <= len(y) else y, words) if words else None # 3. 转大写 upper_case = list(map(str.upper, words)) # 4. 移除所有元音字符 no_vowels = ''.join(filter(lambda c: c.upper() not in {'A','E','I','O','U'}, sentence)) print("以元音开头的单词:", vowel_start) print("最短单词:", shortest) print("大写形式:", upper_case) print("移除元音后的句子:", no_vowels) ``` --- ### 输出示例 ``` 以元音开头的单词: ['is', 'an', 'example!', 'A'] 最短单词: A 大写形式: ['HELLO,', 'THIS', 'IS', 'AN', 'EXAMPLE!', 'A', 'TEST', 'SENTENCE', 'WITH', 'SOME', 'WORDS.'] 移除元音后的句子: Hll, ths s n xmpl! tst sntnc wth sm wrds. ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值