Unrequited Love


Time Limit: 16 Seconds      Memory Limit: 131072 KB

There are n single boys and m single girls. Each of them may love none, one or several of other people unrequitedly and one-sidedly. For the comingq days, each night some of them will come together to hold a single party. In the party, if someone loves all the others, but is not loved by anyone, then he/she is called king/queen of unrequited love.

Input

There are multiple test cases. The first line of the input is an integer T ≈ 50 indicating the number of test cases.

Each test case starts with three positive integers no more than 30000 --nmq. Then each of the nextn lines describes a boy, and each of the nextm lines describes a girl. Each line consists of the name, the number of unrequitedly loved people, and the list of these people's names. Each of the lastq lines describes a single party. It consists of the number of people who attend this party and their names. All people have different names whose lengths are no more than20. But there are no restrictions that all of them are heterosexuals.

Output

For each query, print the number of kings/queens of unrequited love, followed by their names in lexicographical order, separated by a space. Print an empty line after each test case. See sample for more details.

Sample Input

2
2 1 4
BoyA 1 GirlC
BoyB 1 GirlC
GirlC 1 BoyA
2 BoyA BoyB
2 BoyA GirlC
2 BoyB GirlC
3 BoyA BoyB GirlC
2 2 2
H 2 O S
He 0
O 1 H
S 1 H
3 H O S
4 H He O S

Sample Output

0
0
1 BoyB
0 0
0

题目大意:给定一些关系,对于每个人(男孩或女孩),列出他所喜欢的人(允许同性恋),对于每次询问(聚会),求这样一种人:他喜欢所有人,但所有人都不喜欢他

分析:简单分析可知,这种人假如存在,最多只有一个。因为假设有2个这样的人,他们彼此就与题意矛盾。故可以枚举这个人,如何快速枚举?

对于一次聚会,先把第一个人假设为这种人,遍历其后的人,与当前这个人判断关系,若发现这个人不可能是这种人,则把当前遍历的更新为这种人。

扫一遍后,再判断这个人是否真的是,只要和他前面所有的人判断一下即可

#include<cstdio>
#include<string>
#include<set>
#include<map>
using namespace std;
const int N=;
map<string,int> M;
map<string,int>::iterator it;
set< pair<int,int> > S;
string name[N];
int tol,party[N];
char na[];
int hash(char *s){ //关键的一段函数,许多注意点
it=M.find(s);
if(it!=M.end())return it->second;
else {
name[++tol]=s;
return M[s]=tol;
}
}
void Cin(int x){
int i,k,u,v;
for(i=;i<x;i++){
scanf("%s%d",na,&k);
u=hash(na);
while(k--){
scanf("%s",na);
v=hash(na);
S.insert(make_pair(u,v));
}
}
}
int main(){
int T,n,m,q,i,k,ans;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&q);
M.clear(),S.clear(),tol=;
Cin(n),Cin(m);
while(q--){
scanf("%d%s",&k,na);
ans=party[]=M[na];
int p=;
for(i=;i<k;i++){
scanf("%s",na),party[i]=M[na];
if(S.find(make_pair(ans,party[i]))==S.end()||S.find(make_pair(party[i],ans))!=S.end()){ //查找核心
ans=party[i],p=i;
}
}
for(i=;i<p;i++){
if(S.find(make_pair(ans,party[i]))==S.end()||S.find(make_pair(party[i],ans))!=S.end())break;
}
if(i!=p)puts("");
else printf("1 %s\n",name[ans].c_str()); //输出注意点
}
puts("");
}
return ;
}

name[]数组的输出:不能直接写成 printf("1 %s\n",name[ans]),会出错,需要在后面加上“.c_str()”;

map<string,int>类型的容器,前面的string下标也可以用char类型的字符数组表示;

find(map和set都是可以用的)找不到就是==*.end(),找到就是!=*.end();

make_pair的用法,以及 map<string,int>::iterator it; 中it->second的用法

map清空也用clear;

传入函数中的字符数组用了“*”,并且后面没有“[]”;

摘自:http://blog.csdn.net/libing923/article/details/8902193

#include <iostream>
#include<cstdio>
#include<set>
#include<map>
using namespace std;
map<string,int> mp;
set< pair<int,int> > s;
string name[];
int n,m,p,l;
int party[]; int num(char *ch)
{
map<string,int>::iterator it;
it=mp.find(ch);
if (it!=mp.end()) return mp[ch];
else
{
name[++l]=ch;
mp[ch]=l;
return l;
}
}
void read(int n)
{
char ch[],str[];
for(int i=;i<=n;i++)
{
int k;
scanf("%s%d",&ch,&k);
int u=num(ch);
for(;k>;k--)
{
scanf("%s",&str);
int v=num(str);
s.insert(make_pair(u,v));
}
}
return;
}
int main()
{
int t;
while(~scanf("%d",&t))
{
for(;t>;t--)
{
scanf("%d%d%d",&n,&m,&p);
l=; mp.clear(); s.clear();
read(n);
read(m);
for(int i=;i<=p;i++)
{
int k;
char ch[];
scanf("%d",&k);
for(int j=;j<=k;j++)
{
scanf("%s",&ch);
party[j]=mp[ch];
}
bool flag;
for(int j=;j<=k;j++)
{
flag=;
for(int z=;z<=k;z++)
{
if (j==z) continue;
if (s.find(make_pair(party[j],party[z]))==s.end()) {flag=;break;}
if (s.find(make_pair(party[z],party[j]))!=s.end()) {flag=;break;}
}
if (flag) {printf("1 %s\n",name[party[j]].c_str());break;}
}
if (!flag) printf("0\n");
}
printf("\n");
}
}
return ;
}

上面是自己写的,在上一个程序的借鉴上。

set, map, string, find(), string name[100],等的混合的更多相关文章

  1. No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, org.springframework.boot.logging.LogLevel>]

    java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...

  2. Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String, java.lang.String>

    org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'log ...

  3. No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.String>]

    java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...

  4. JAVA中List转换String,String转换List,Map转换String,String转换Map之间的转换类

    <pre name="code" class="java"></pre><pre name="code" cl ...

  5. Java基础知识强化之集合框架笔记54:Map集合之HashMap集合(HashMap<String,String>)的案例

    1. HashMap集合 HashMap集合(HashMap<String,String>)的案例 2. 代码示例: package cn.itcast_02; import java.u ...

  6. sprinbcloud学习之-Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String>

    日志报错,提示Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String>, 原因为 ...

  7. List<Map<String, String>>和Map<String, List<String>>遍历

    public void TestM() {     List<Map<String, String>> lm = new ArrayList<>();     Ma ...

  8. 原型模式 private static Map<String,Prototype> map = new HashMap<String,Prototype>();

    public class PrototypeManager { /** * 用来记录原型的编号和原型实例的对应关系 */ private static Map<String,Prototype& ...

  9. Map 集合 和 String 字符串相互转换工具类

    package com.skynet.rimp.common.utils.util; import java.util.Arrays; import java.util.HashMap; import ...

  10. mybatis用Map<Long,List<String>>作为参数

    mapper.xml文件里的<insert id="insertByMap" parameterType="java.util.Map"> inse ...

随机推荐

  1. golang的极简流式编程实现

    传统的过程编码方式带来的弊端是显而易见,我们经常有这样的经验,一段时间不维护的代码或者别人的代码,突然拉回来看需要花费较长的时间,理解原来的思路,如果此时有个文档或者注释写的很好的话,可能花的时间会短 ...

  2. RS232串口通信

    RS232串口经常使用在PC机与FPGA通信中,用于两者之间的数据传输,因为UART协议简单.易实现,故经常使用. DB9接口只需要使用3根线,RXD(2).TXD(3)和GND(5),如下图所示.而 ...

  3. Python(输入、输出;简单运算符;流程控制;转译)

    一 输入输出 python3中统一都是input,python2中有raw_input等同于python3的input,另外python2中也有input 1.res=input("pyth ...

  4. django基础2: 路由配置系统,URLconf的正则字符串参数,命名空间模式,View(视图),Request对象,Response对象,JsonResponse对象,Template模板系统

    Django基础二 request request这个参数1. 封装了所有跟请求相关的数据,是一个对象 2. 目前我们学过1. request.method GET,POST ...2. reques ...

  5. http post url参数封装(key token 及校验码)

    post请求本来是一种很常见的web请求方式,相信许多项目都有一系列的封装工具类. 今天遇着一个特殊的需求. 需要在post的请求url内封装相应的token 与及key相关的值,这就奇怪了,url封 ...

  6. ThinkPHP框架基础知识三

    一.JS文件与Css文件存放位置 其实JS与Css文件放在任意位置都可以找到,只要路径正确就行. 在TP框架中我们访问的所有文件都要走入口文件index.php,相当于访问的是index.php页面. ...

  7. webform中Repeater的Command用法、Repeater的替代方法

    Command: 在Repeater控件循环执行过程中,可以给每一项的某个按钮或其他控件设置CommandName.CommandArgument属性,用于在后台代码中获取单项 数据进行调用.   需 ...

  8. $用ConfigParser模块读写conf配置文件

    ConfigParser是Python内置的一个读取配置文件的模块,用它来读取和修改配置文件非常方便,本文介绍一下它的基本用法. 数据准备 假设当前目录下有一个名为sys.conf的配置文件,其内容如 ...

  9. linux中shell变量$#,$@,$0,$1,$2的含义

    linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...

  10. CSS 边距和填充

    margin and padding are the two most commonly used properties for spacing-out elements. A margin is t ...