string基本函数运用。HOJ2206 Defining Moment。
string中 find, rfind, substr, insert函数的运用。
| Time limit: | 1sec. | Submitted: | 86 |
| Memory limit: | 64M | Accepted: | 47 |
As a homework assignment, you have been tasked with creating a program that provides the meanings for many different words. As you dislike the idea of writing a program that just prints definitions of words, you decide to write a program that can print definitions of many variations of just a handful of different root words. You do this by recognizing common prefixes and suffixes. Since your program is smart enough to recognize up to one prefix and one suffix per word, it can process many forms of each word, significantly reducing the number of rote definitions required.
For this problem, you'll be writing the prefix/suffix processing portion of the program.
Valid prefixes and their meanings are:
anti<word> |
against <word> |
post<word> |
after <word> |
pre<word> |
before <word> |
re<word> |
<word> again |
un<word> |
not <word> |
Valid suffixes and their meanings are:
<word>er |
one who <word>s |
<word>ing |
to actively <word> |
<word>ize |
change into <word> |
<word>s |
multiple instances of <word> |
<word>tion |
the process of <word>ing |
Note that suffixes are tied more tightly to their root word and should therefore be expanded last. For example, the word "vaporize" would be expanded through the following steps:
unvaporize not vaporize not change into vapor
Of course, the definitions are not exactly right, but how much polish does the professor expect for a single homework grade?
Input
Input to this problem will begin with a line containing a single integer n indicating the number of words to define. Each of the following n lines will contain a single word. You need to expand at most one prefix and one suffix, and each word is guaranteed to have a non-empty root (i.e., if the prefix and/or suffix are removed, a non-empty string will remain). Each word will be composed of no more than 100 printable characters.
Output
For each word in the input, output the expanded form of the word by replacing the prefix and/or suffix with its meaning.
Sample Input
6 vaporize prewar recooking root repopularize uninforming
Sample Output
change into vapor before war to actively cook again root change into popular again not to actively inform
说明:
给出一个单词。 找出它的前缀与后缀。 然后翻译它。
应先翻译前缀。 即后缀解释距离词根更近。
rfind()函数返回从后往前第一次找到某子串的位置。未找到返回-1。
代码如下:#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
while (n--) {
string w;
cin >> w;
bool reflag = 0;
int l = w.length();
if (w.find("anti") == 0) {
w = w.substr(4, l - 4);
cout << "against ";
} else if (w.find("post") == 0) {
w = w.substr(4, l - 4);
cout << "after ";
} else if (w.find("pre") == 0) {
w = w.substr(3, l - 3);
cout << "before ";
} else if (w.find("re") == 0) {
w = w.substr(2, l - 2);
reflag = 1;
} else if (w.find("un") == 0) {
w = w.substr(2, l - 2);
cout << "not ";
}
l = w.length();
if (l - 2 > 0 && w.rfind("er") == l - 2) {
w = w.substr(0, l - 2);
w.insert(0, "one who ");
w += "s";
} else if (l - 3 > 0 && w.rfind("ing") == l - 3) {
w = w.substr(0, l - 3);
w.insert(0, "to actively ");
} else if (l - 3 > 0 && w.rfind("ize") == l - 3) {
w = w.substr(0, l - 3);
w.insert(0, "change into ");
} else if (l - 1 > 0 && w.rfind("s") == l - 1) {
w = w.substr(0, l - 1);
w.insert(0, "multiple instances of ");
} else if (l - 4 > 0 && w.rfind("tion") == l - 4) {
w = w.substr(0, l - 4);
w.insert(0, "the process of ");
w += "ing";
}
cout << w;
if (reflag)cout << " again";
cout << endl;
}
return 0;
}

浙公网安备 33010602011771号