http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem&problem=350
题意:
输入nKey行关键字,输入nCase行借口,判断哪行借口包含最多关键字。有多个时同时输出。
解题:
在借口字符串中找关键字,并统计好。注:只有连续的单词才能组成关键字,即"abca ca se"中,只包含一次关键字"ca",而不是两次。
为了把单词抽出,把所以非字母字符转变成空格,再用sstream把一个个单独单词分离出来,对比即可。
(开始WA的,后来在厕所想到思路。。。)
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <string.h>
#include <stdio.h>
using namespace std;
const int MAXN = 20;
void fun_str(string &str)
{
for ( int i=0; i<str.length(); i++ )
{
if ( isalpha(str[i]) )
{
str[i] = tolower(str[i]);
} // end if
else
{
str[i] = ' ';
} // end else
} // end for
}
int main()
{
int nKey, nCase;
int index = 0;
while ( cin >>nKey >>nCase )
{
index++;
cin.ignore();
vector <string> vKey;
vector <string> vExcuses;
vector <string> vExcusesEx;
string strIn;
for ( int i=0; i<nKey; i++ )
{
getline(cin, strIn);
vKey.push_back(strIn);
} // end for
for ( int i=0; i<nCase; i++ )
{
getline(cin, strIn);
vExcuses.push_back(strIn);
fun_str(strIn);
vExcusesEx.push_back(strIn);
} // end for
int szCount[MAXN];
memset(szCount, 0, sizeof(szCount));
int max = -1;
for ( int i=0; i<nCase; i++ )
{
istringstream strStream(vExcusesEx[i]);
string strTmp;
while ( strStream >>strTmp )
{
for ( int j=0; j<nKey; j++ )
{
if ( strTmp == vKey[j] )
{
szCount[i]++;
} // end if
} // end for
} // end while
if ( szCount[i] > max )
{
max = szCount[i];
} // end if
} // end for
cout <<"Excuse Set #" <<index <<'\n';
for ( int i=0; i<nCase; i++ )
{
if ( szCount[i] == max )
{
cout <<vExcuses[i] <<'\n';
} // end if
} // end for
cout <<endl;
} // end while
return 0;
}