思路:用map将每个字符串与一个数字进行对应,然后暴力统计就好了

#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#include<string>
#define Maxn 2010
using namespace std;
int fr[Maxn][Maxn],mul[Maxn][Maxn],Max[Maxn];
char str[Maxn][];
map<string,int> g;
vector<int> head[Maxn*];
vector<int> ans[Maxn*];
int cmp(int a,int b)
{
return strcmp(str[a],str[b])<;
}
int main()
{
int t,n,q,i,j,k,Case=,u,v;
scanf("%d",&t);
char s1[],s2[];
while(t--)
{
memset(fr,,sizeof(fr));
memset(mul,,sizeof(mul));
memset(Max,,sizeof(Max));
g.clear();
scanf("%d%d",&n,&q);
for(i=;i<=*n;i++)
head[i].clear(),ans[i].clear();
int cnt=;
for(i=;i<=n;i++)
{
scanf("%s %s",&s1,&s2);
if(!g[s1]){
g[s1]=++cnt;
strcpy(str[cnt],s1);
}
if(!g[s2]){
g[s2]=++cnt;
strcpy(str[cnt],s2);
}
u=g[s1],v=g[s2];
head[u].push_back(v);
head[v].push_back(u);
fr[u][v]=fr[v][u]=;
}
int sz;
for(i=;i<=cnt;i++){
sz=head[i].size();
for(j=;j<sz;j++){
u=head[i][j];
int ss=head[u].size();
for(k=;k<ss;k++){
v=head[u][k];
if(!fr[i][v]&&i!=v)
mul[i][v]++,Max[i]=max(Max[i],mul[i][v]);
}
}
}
for(i=;i<=cnt;i++){
for(j=;j<=cnt;j++){
if(j==i) continue;
if(mul[i][j]==Max[i]&&Max[i])
ans[i].push_back(j);
}
sort(ans[i].begin(),ans[i].end(),cmp);
}
printf("Case %d:\n",++Case);
//cout<<q<<endl;
for(i=;i<=q;i++){
scanf("%s",s1);
u=g[s1];
if(ans[u].empty())
printf("-");
else{
sz=ans[u].size();
v=ans[u][];
printf("%s",str[v]);
for(j=;j<sz;j++){
v=ans[u][j];
printf(" %s",str[v]);
}
}
printf("\n");
}
}
return ;
}

hdu 4039 暴力的更多相关文章

  1. hdu 4039 The Social Network

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4039 题目分类:字符串+bfs 题意:给一个人际关系图,根据关系图,给一个人推荐一个人认识 题目分析: ...

  2. HDU 6351暴力枚举 6354计算几何

    Beautiful Now Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

  3. HDU 5944 暴力

    Fxx and string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)T ...

  4. hdu 4952 暴力

    http://acm.hdu.edu.cn/showproblem.php?pid=4952 给定x,k,i从1到k,每次a[i]要是i的倍数,并且a[i]大于等于a[i-1],x为a0 递推到下一个 ...

  5. hdu 4708(暴力+找规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4708 思路:由于N不大,并且我们可以发现通过旋转得到的4个对角线的点的位置关系,以及所要旋转的最小步数 ...

  6. HDU - 6183 暴力,线段树动态开点,cdq分治

    B - Color itHDU - 6183 题目大意:有三种操作,0是清空所有点,1是给点(x,y)涂上颜色c,2是查询满足1<=a<=x,y1<=b<=y2的(a,b)点一 ...

  7. HDU 5386 暴力

    题目 也是个坑题,可惜没有发现这是个水题,被矩阵的气势吓住了,其实后来做出来的人挺多,就应该想到没那么难了.(两个队友陷入DP无法自拔,没有想换题的打算). 题意:告诉初始矩阵,目的矩阵,告诉n个步骤 ...

  8. hdu 4039 2011成都赛区网络赛I ***

    两层搜索,直接for循环就行了,还要注意不能是自己的朋友 #include<cstdio> #include<iostream> #include<algorithm&g ...

  9. hdu 4007 暴力or线段树 ***

    尼玛,INF不能定义太大,找标程对拍了好久 #include<cstdio> #include<iostream> #include<algorithm> #inc ...

随机推荐

  1. HttpRequest Get Post,WebClient Get

    #region HttpRequestGet public string HttpRequestGet(string url) { return HttpRequestGet(url, WebRequ ...

  2. contentWindow 和contentDocument区别 及iframe访问

    a>contentWindow 兼容各个浏览器,可取得子窗口的 window 对象. b>contentDocument Firefox 支持,> ie8 的ie支持.可取得子窗口的 ...

  3. Combox和DropDownList控件的区别

    共同点:都是下拉框控件 不同点:Combox用在winform上,DropDownList用在网页上,且两者绑定方式略有不同 绑定数据例子如下—— 1.Combox绑定 DataTable dtBus ...

  4. 汉字简体繁体转换----Javascript

    最近看到有个简体--繁体字互相转换的程序,是用JS实现的,感觉很好玩,所以拿来研究研究.先看看界面如下: 汉字简体繁体转换 // 0&&parent.frames.length) { ...

  5. uva387 - A Puzzling Problem

    A Puzzling Problem The goal of this problem is to write a program which will take from 1 to 5 puzzle ...

  6. uva140 - Bandwidth

    Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orderi ...

  7. 书写CSS需要注意的地方

    1.注意对图片设置宽高和转化为块2.文字超出的设置3.空白部分用空div来设置4.做之前考虑重用,重用部分命名不要和内容相关 尽量公共(comWidth area small big img list ...

  8. 【转】selenium及webdriver的原理

    主要内容转自:http://blog.csdn.net/ant_ren/article/details/7968582和http://blog.csdn.net/ant_ren/article/det ...

  9. Codeforces Round #308 (Div. 2) C. Vanya and Scales dfs

    C. Vanya and Scales Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/552/p ...

  10. (字符串的处理4.7.16)POJ 1159 Palindrome(让一个字符串变成回文串需要插入多少个字符...先逆序,在减去公共子序列的最大长度即可)

    /* * POJ_1159.cpp * * Created on: 2013年10月29日 * Author: Administrator */ #include <iostream> # ...