很早就碰到的一道题,结果纠结了真心久,当时什么都不会:STL不会,快排不会,二分查找不会,当时根本就拿这道题没有办法。后来才发现这道题其实挺简单的。
常见方法大概有两种,一是快排后二分查找,二是用set来做。时间消耗上两者差别不大,前者看起来较有技术含量,后者代码简单易懂,这里贴出后者的代码。
Run Time: 0.26sec
Run Memory: 1368KB
Code length: 1054Bytes
SubmitTime: 2011-11-14 01:02:09
// Problem#: 1194
// Submission#: 968179
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <string>
#include <set>
using namespace std;
int main()
{
int n, m;
int i, j;
int sum;
string friends, received;
while ( true ) {
cin >> n;
if ( n == 0 )
break;
cin >> m;
set<string> send;
for ( i = 0; i < n; i++ ) {
cin >> friends;
for ( j = 0; j < friends.size(); j++ ) {
if ( friends[ j ] >= 'A' && friends[ j ] <= 'Z' )
friends[ j ] += 32;
}
send.insert( friends );
}
sum = 0;
for ( i = 0; i < m; i++ ) {
cin >> received;
for ( j = 0; j < received.size(); j++ ) {
if ( received[ j ] >= 'A' && received[ j ] <= 'Z' )
received[ j ] += 32;
}
if ( send.find( received ) != send.end() )
send.erase( send.find( received ) );
}
cout << send.size() << endl;
}
return 0;
}