36th Chengdu Regional Tournament hdoj4039 The social network (map + string processing)

Source: Internet
Author: User
Tags number strings


This problem is one of the Chengdu Regional Competition online game.

This idea is very simple, but from time to consider, it is best not to use the matrix storage, I use the chain forward to the star.

Use online queries. Use map to number strings, because it is convenient. To recommend a friend, is actually a friend of friends (here refers to direct friends, the figure refers to the direct side of the link).

So when looking, just need to find friends and count them.

Note: You cannot have a space for the output.

Attached code:

#include <iostream>using namespace std #include <cstdio> #include <cstdlib> #include <cstring> #include <map> #include <algorithm>int n,m;char s11[20],s22[20];string g[20100],l[20100];int next[201000]     , Head[2010],key[201000];int num;void Add (int u,int v) {key[num]=v;     Next[num]=head[u]; head[u]=num++;}    int main () {int t,pp=0;    scanf ("%d", &t);    while (t--) {map<string,int> hash;          int n,m,i,j,k;    String s1,s2;        int cnt=0;    num=0;    scanf ("%d%d", &n,&m);        memset (head,-1,sizeof (head));        for (i=0;i<n;++i) {scanf ("%s%s", S11,S22);        S1=string (S11);        S2=string (S22);        if (hash[s1]==0) hash[s1]=++cnt,l[cnt]=s1;                if (hash[s2]==0) hash[s2]=++cnt,l[cnt]=s2;        Add (Hash[s1],hash[s2]);        Add (Hash[s2],hash[s1]);                } printf ("Case%d:\n", ++pp);        for (i=0;i<m;++i) {scanf ("%s", S11); S1=string (S11);        int P=HASH[S1];        int f[20100],flag[20010];        memset (F,0,sizeof (f));                memset (flag,0,sizeof (flag));                for (K=head[p];k!=-1;k=next[k]) flag[key[k]]=-1; for (K=head[p];k!=-1;k=next[k]) if (key[k]!=p) {for (J=head[key[k]];j!=-1;j=next[j]                      ) if (Key[j]!=key[k] && key[j]!=p && flag[key[j]]==0) {                   f[key[j]]++;        }} int max=-1;          for (k=1;k<=cnt;++k) {Max=max (max,f[k]);            printf ("%d\n", F[k]);           } if (max==0) {printf ("-\n");           Continue        } int q=0;               for (K=1;k<=cnt;++k) if (Max==f[k]) {g[q++]=l[k];               } sort (g,g+q);        for (k=0;k<q-1;++k) cout << g[k] << "";        cout << G[q-1]; Cout << Endl; }} return 0;}

36th Chengdu Regional Tournament hdoj4039 The social network (map + string processing)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.