Problem Description
Mrs. Deli is running the delicatessen store "Deli Deli". Last year Mrs. Deli has decided to expand her business and build up an online store. She has hired a programmer who has implemented the online store.
Recently some of her new online customers complained about the electronic bills. The programmer had forgotten to use the plural form in case that an item is purchased multiple times. Unfortunaly the programmer of Mrs. Deli is on holiday and now it is your task to implement this feature for Mrs. Deli. Here is a description how to make the plural form:
1. If the word is in the list of irregular words replace it with the given plural.
2. Else if the word ends in a consonant followed by "y", replace "y" with "ies".
3. Else if the word ends in "o", "s", "ch", "sh" or "x", append "es" to the word.
4. Else append "s" to the word.
Recently some of her new online customers complained about the electronic bills. The programmer had forgotten to use the plural form in case that an item is purchased multiple times. Unfortunaly the programmer of Mrs. Deli is on holiday and now it is your task to implement this feature for Mrs. Deli. Here is a description how to make the plural form:
1. If the word is in the list of irregular words replace it with the given plural.
2. Else if the word ends in a consonant followed by "y", replace "y" with "ies".
3. Else if the word ends in "o", "s", "ch", "sh" or "x", append "es" to the word.
4. Else append "s" to the word.
Input
The first line of the input file consists of two integers L and N (0 ≤ L ≤ 20, 1 ≤ N ≤ 100). The following L lines contain the description of the irregular words and their plural form. Each line consists of two words separated by
a space character, where the first word is the singular, the second word the plural form of some irregular word. After the list of irregular words, the following N lines contain one word each, which you have to make plural. You may assume that each word consists
of at most 20 lowercase letters from the english alphabet ('a' to 'z').
Output
Print N lines of output, where the ith line is the plural form of the ith input word.
Sample Input
3 7 rice rice spaghetti spaghetti octopus octopi rice lobster spaghetti strawberry octopus peach turkey
Sample Output
rice lobsters spaghetti strawberries octopi peaches turkeys
http://acm.hdu.edu.cn/showproblem.php?pid=1804
#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;
string fun(string s){
if (s[s.size() - 1] == 'y' && (s[s.size() - 2] != 'a' && s[s.size() - 2] != 'e' && s[s.size() - 2] != 'i' && s[s.size() - 2] != 'o' && s[s.size() - 2] != 'u')){
s=s.erase(s.size() - 1, 1);
s = s + "ies";
return s;
}
else if (s[s.size() - 1] == 'x' || s[s.size() - 1] == 's' || s[s.size() - 1] == 'o'){
s += "es";
return s;
}
else if (s[s.size() - 1] == 'h' && ((s[s.size() - 2] == 'c') || (s[s.size() - 2] == 's'))){
s += "es";
return s;
}
else{
s += "s";
return s;
}
}
int main(){
map<string,string> a;
vector<string> s;
string str1,str2;
int n, m;
cin >> n >> m;
while (n--){
cin >> str1>>str2;
a.insert(make_pair(str1, str2));
}
cin.ignore(1, '\n');
while (m--){
getline(cin, str1);
s.push_back(str1);
}
for (int i = 0; i < s.size(); i++){
if (a[s[i]] == "\0")
cout << fun(s[i]) << endl;
else
cout << a[s[i]] << endl;
}
}map(映射),一一对应的容器 ,可以通过用[ ]访问第二个元素,如果没有与之对应的元素,则返回"\0".
有多个if的时候,注意运算结果的if里的条件是否满足需要
本文介绍了一个程序设计任务:为英语单词自动生成正确的复数形式。该任务涉及处理不规则变化的单词以及规则变化的几种情况。文章提供了一段C++代码实现,并解释了如何根据单词结尾字母来确定其复数形式。
1336

被折叠的 条评论
为什么被折叠?



