【STL】Saving the Universe

Saving the Universe
Description
The urban legend goes that if you go to the Google homepage and search for "Google", the universe will implode. We have a secret to share... It is true! Please don't try it, or tell anyone. All right, maybe not. We are just kidding.

The same is not true for a universe far far away. In that universe, if you search on any search engine for that search engine's name, the universe does implode!

To combat this, people came up with an interesting solution. All queries are pooled together. They are passed to a central system that decides which query goes to which search engine. The central system sends a series of queries to one search engine, and can switch to another at any time. Queries must be processed in the order they're received. The central system must never send a query to a search engine whose name matches the query. In order to reduce costs, the number of switches should be minimized.

Your task is to tell us how many times the central system will have to switch between search engines, assuming that we program it optimally.

Input
The first line of the input file contains the number of cases, N. N test cases follow.

Each case starts with the number S -- the number of search engines. The next S lines each contain the name of a search engine. Each search engine name is no more than one hundred characters long and contains only uppercase letters, lowercase letters, spaces, and numbers. There will not be two search engines with the same name.

The following line contains a number Q -- the number of incoming queries. The next Q lines will each contain a query. Each query will be the name of a search engine in the case.

Limits:
0 < N ≤ 20
2 ≤ S ≤ 100
0 ≤ Q ≤ 1000

Output
For each input case, you should output:

Case #X: Y

where X is the number of the test case and Y is the number of search engine switches. Do not count the initial choice of a search engine as a switch.

Sample Input
Original Transformed
2
5
Yeehaw
NSM
Dont Ask
B9
Googol
10
Yeehaw
Yeehaw
Googol
B9
Googol
NSM
B9
NSM
Dont Ask
Googol
5
Yeehaw
NSM
Dont Ask
B9
Googol
7
Googol
Dont Ask
NSM
NSM
Yeehaw
Yeehaw
Googol

Sample Output
Original Transformed
Case #1: 1
Case #2: 0

#if __GNUC__>2
#include <ext/hash_set>
#include <ext/hash_map>
using namespace __gnu_cxx;
#else
#include <hash_set>
#include <hash_map>
using namespace stdext;
#endif
//hash_set的头文件

#include<cstdio>
#include<string>
using namespace std;
/*-------------------------------------------*/
/*函数类
 *作为hash_map的hash函数 
 *string没有默认的hash函数 
 */ 
class str_hash
{
public:
    size_t operator()(const string& str) const
    {
        unsigned long __h = 0;
        for (size_t i = 0 ; i < str.size() ; ++i)
            __h = 5*__h + str[i];
        return size_t(__h);
    }
};
/*-------------------------------------------*/
/*函数类 
 *作为hash_map的比较函数 )
 *(查找的时候不同的key往往可能对用到相同的hash值
*/ 
class str_compare
{
public:
    bool operator()(const string& str1,const string& str2)const
    {
        return   str1==str2;
    }
};
/*-------------------------------------------*/
int main()
{
    int T,n,m;
    string s;
    char ch[105];
    scanf("%d", &T);
    for(int t=1; t<=T; ++t)
    {
        scanf("%d", &n);
        getchar();
        for(int i=0; i<n; ++i)
            gets(ch);
        scanf("%d", &m);
        getchar();
        hash_set<string,str_hash,str_compare> st;
        int count = 0;
        for(int i=0; i<m; ++i)
        {
            gets(ch);
            s=string(ch);
            if (st.find(s) == st.end())
            {
                if (st.size() == n-1)
                {
                    st.clear();
                    count++;
                }
                st.insert(s);
            }
        }
        printf("Case #%d: %d\n", t, count);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值