Judge Ito is having a problem with people subpoenaed for jury duty giving rather lame excuses in order to avoid serving. In order to reduce the amount of time required listening to goofy excuses, Judge Ito has asked that you write a program that will search for a list of keywords in a list of excuses identifying lame excuses. Keywords can be matched in an excuse regardless of case.
Input to your program will consist of multiple sets of data.
SPMamp
".,!?&] not including the square brackets and will not exceed 70 characters in length.For each input set, you are to print the worst excuse(s) from the list.
For each set of input, you are to print a single line with the number of the set immediately after the string ``Excuse Set #". (See the Sample Output). The following line(s) is/are to contain the worst excuse(s) one per line exactly as read in. If there is more than one worst excuse, you may print them in any order.
After each set of output, you should print a blank line.
5 3 dog ate homework canary died My dog ate my homework. Can you believe my dog died after eating my canary... AND MY HOMEWORK? This excuse is so good that it contain 0 keywords. 6 5 superhighway crazy thermonuclear bedroom war building I am having a superhighway built in my bedroom. I am actually crazy. 1234567890.....,,,,,0987654321?????!!!!!! There was a thermonuclear war! I ate my dog, my canary, and my homework ... note outdated keywords?
Excuse Set #1 Can you believe my dog died after eating my canary... AND MY HOMEWORK? Excuse Set #2 I am having a superhighway built in my bedroom. There was a thermonuclear war!
字符串操作,主要读取一行字符的时候需要getline函数,cin遇到空格会退出。还要是用getchar()来吸收来自上次IO操作中缓存的字符。
#include<iostream> #include<vector> #include<string> #include<cctype> using namespace std; int find(vector<string> a,string b){ vector<string>vec3; for (string::size_type i = 0; i < b.size(); i++){ string s2; for (string::size_type j = i; j < b.size(); j++){ if (!isalpha(b[j])){ break; } else{ s2 += tolower(b[j]); } } if (!s2.empty())vec3.push_back(s2); } vector<string>::iterator i,j; int cnt = 0; for (i = a.begin(); i != a.end(); i++){ for (j = vec3.begin(); j != vec3.end(); j++){ if (*i == *j){ cnt++; } } } return cnt; } int main(){ int m, n; int num = 1; vector<string>vec1, vec2; while (cin >> m >> n){ vec1.clear(); vec2.clear(); for (int i = 0; i < m; i++){ string s1; cin >> s1; vec1.push_back(s1); } getchar(); for (int i = 0; i < n; i++){ string s2; getline(cin,s2); vec2.push_back(s2); } int max = 0; vector<string> vec4; for (int i = 0; i < vec2.size(); i++){ int tmp = find(vec1,vec2[i]); if (tmp>max){ max=tmp; vec4.clear(); vec4.push_back(vec2[i]); } else if (tmp == max){ vec4.push_back(vec2[i]); } } cout << "Excuse Set #" << num++ << endl; vector<string>::iterator i; for (i = vec4.begin(); i != vec4.end(); i++){ cout <<*i << endl; } cout << endl; } return 0; }