数据结构模块---- (- o -)


Message Flood

Time Limit: 1500MS   Memory Limit: 65536KB

Problem Description

Well, how do you feel about mobile phone? Your answer would probably be something like that "It's so convenient and benefits people a lot". However, If you ask Merlin this question on the New Year's Eve, he will definitely answer "What a trouble! I have to keep my fingers moving on the phone the whole night, because I have so many greeting message to send!" Yes, Merlin has such a long name list of his friends, and he would like to send a greeting message to each of them. What's worse, Merlin has another long name list of senders that have sent message to him, and he doesn't want to send another message to bother them Merlin is so polite that he always replies each message he receives immediately). So, before he begins to send message, he needs to figure to how many friends are left to be sent. Please write a program to help him. Here is something that you should note. First, Merlin's friend list is not ordered, and each name is alphabetic strings and case insensitive. These names are guaranteed to be not duplicated. Second, some senders may send more than one message to Merlin, therefore the sender list may be duplicated. Third, Merlin is known by so many people, that's why some message senders are even not included in his friend list.

Input

There are multiple test cases. In each case, at the first line there are two numbers n and m (1<=n,m<=20000), which is the number of friends and the number of messages he has received. And then there are n lines of alphabetic strings(the length of each will be less than 10), indicating the names of Merlin's friends, one per line. After that there are m lines of alphabetic strings, which are the names of message senders. The input is terminated by n=0.

Output

For each case, print one integer in one line which indicates the number of left friends he must send.

Example Input

5 3
Inkfish
Henry
Carp
Max
Jericho
Carp
Max
Carp
0


数据结构实验之数组二:稀疏矩阵

Time Limit: 5MS Memory Limit: 1000KB

Problem Description

对于一个n*n的稀疏矩阵M(1 <= n <= 1000),采用三元组顺序表存储表示,查找从键盘输入的某个非零数据是否在稀疏矩阵中,如果存在则输出OK,不存在则输出ERROR。稀疏矩阵示例图如下:

Input

连续输入多组数据,每组数据的第一行是三个整数mu, nu, tu(tu<=50),分别表示稀疏矩阵的行数、列数和矩阵中非零元素的个数,数据之间用空格间隔,随后tu行输入稀疏矩阵的非零元素所在的行、列值和非零元素的值,每组数据的最后一行输入要查询的数据k。

Output

 输出查询结果,查找成功输出 OK ,找不到输出 ERROR。

Example Input

3 5 51 2 141 5 -52 2 -73 1 363 4 2836

Example Output

OK

  1. #include <iostream>  
  2. #include <algorithm>  
  3.   
  4. using namespace std;  
  5.   
  6. typedef struct  
  7. {  
  8.     int x,y,z;  
  9. }Arry;  
  10. int main()  
  11. {  
  12.     ios::sync_with_stdio(false);  
  13.     int n, m, j;  
  14.     int mu, tu, nu, key;  
  15.     Arry A[1001];  
  16.     while(cin>>mu>>nu>>tu)  
  17.     {  
  18.         int flag=0;  
  19.         for(int i=0;i<tu;i++)  
  20.         {  
  21.             cin>>n>>m>>j;  
  22.             A[i].x=n;  
  23.             A[i].y=m;  
  24.             A[i].z=j;  
  25.         }  
  26.         cin>>key;  
  27.         for(int i=0;i<tu;i++)  
  28.         {  
  29.             if(A[i].z==key)  
  30.             {  
  31.                 flag=1;  
  32.                 break;  
  33.             }  
  34.         }  
  35.         if(!flag)  
  36.             cout<<"ERROR"<<endl;  
  37.         else  
  38.             cout<<"OK"<<endl;  
  39.   
  40.     }  

树结构练习——判断给定森林中有多少棵树(并查集)

Time Limit: 1000MS   Memory Limit: 65536KB

Problem Description

 众人皆知,在编程领域中,C++是一门非常重要的语言,不仅仅因为其强大的功能,还因为它是很多其他面向对象语言的祖先和典范。不过这世上几乎没什么东西是完美的,C++也不例外,多继承结构在带来强大功能的同时也给软件设计和维护带来了很多困难。为此,在java语言中,只允许单继承结构,并采用接口来模拟多继承。KK最近获得了一份java编写的迷你游戏的源代码,他对这份代码非常感兴趣。这份java代码是由n个类组成的(本题不考虑接口),现在,他想要知道这份代码中有多少个类没有直接基类。n个类分别用数字1..n表示。
 

Input

 输入数据包含多组,每组数据格式如下。
第一行包含两个整数n,m,表示该份代码中的n个类和m个单继承关系。
后面m行,每行两个整数a b,表示a是b的直接基类。

Output

 对于每组输入,输出该组数据中有多少个类没有直接基类。每组输出占一行。
 

Example Input

2 1
1 2
2 0

Example Output

1
2

01 /*
02 将森林转化成二叉树具体方法:
03     ① 将森林中的每棵树变为二叉树
04   ② 因为转换所得的二叉树的根结点的右子树均为空,故可将
05  各二叉树的根结点视为兄弟从左至右连在一起,就形成了一棵二叉树。
06 */
07 //由上面很容易看出但凡是将森林分成树然后讲每棵树转化成二叉树其根节点等于其本身。
08  
09 #include<bits/stdc++.h>
10 using namespace std;
11 int bing[1010];
12 int f(int x)
13 {
14 return x==bing[x]?x:bing[x]=f(bing[x]);
15 }
16 void me(int x,int y)
17 {
18 if(f(x)!=f(y))
19 bing[f(x)]=f(y);
20 }
21 int main()
22 {
23 int n,m,i,x,y,cnt;
24 while(cin>>n>>m)
25 {
26 cnt=0;
27 for(i=0;i<=n;i++)
28 bing[i]=i;
29 for(i=0;i<m;i++)
30 {
31 cin>>x>>y;
32 me(x,y);
33 }
34 for(i=1;i<=n;i++)
35 {
36 if(bing[i]==i)cnt++;
37 }
38 cout<<cnt<<endl;
39 }
40 return 0;
41 }

数据结构实验之图论六:村村通公路(最小生成树prim)

prim算法思路:

     任意一个顶点 u 作为生成树的根,

     之后往生成树上添加新的顶点 v在添加的顶点 w 和已经在生成树上的顶点v 之间必定存在一条边,

并且该边的权值在所有连通顶点 u和 v 之间的边中取值最小

     之后继续往生成树上添加顶点,直至生成树上含有 n-1 个顶点为止。

Time Limit: 1000MS   Memory Limit: 65536KB

Problem Description

当前农村公路建设正如火如荼的展开,某乡镇政府决定实现村村通公路,工程师现有各个村落之间的原始道路统计数据表,表中列出了各村之间可以建设公路的若干条道路的成本,你的任务是根据给出的数据表,求使得每个村都有公路连通所需要的最低成本。

Input

连续多组数据输入,每组数据包括村落数目N(N <= 1000)和可供选择的道路数目M(M <= 3000),随后M行对应M条道路,每行给出3个正整数,分别是该条道路直接连通的两个村庄的编号和修建该道路的预算成本,村庄从1~N编号。 

Output

输出使每个村庄都有公路连通所需要的最低成本,如果输入数据不能使所有村庄畅通,则输出-1,表示有些村庄之间没有路连通。 

Example Input

5 8
1 2 12
1 3 9
1 4 11
1 5 3
2 3 6
2 4 9
3 4 4
4 5 6

Example Output

19


   
   
01 #include <bits/stdc++.h>
02 #define inf 0x3f3f3f3f
03 using namespace std;
04 int mmp[1000][1000];
05 //用来存图,
06 int vis[1010],dis[1010];
07 //vis标记数组。dis 数组用来记录当前生成树到每个节点的距离
08 int sum, n, m;
09 void prim()
10 {
11     int i,j,mm;
12     sum=0;
13     //sum生成树总权值。
14     memset(vis,0,sizeof(vis));
15     //因为村庄从1~n所以从1-n进行编号
16     for(i=1; i<=n; i++)
17         dis[i]=mmp[1][i];
18     //距离。出发点到其他点。
19     vis[1]=1;
20     int pos,d=0;
21     //pos记录节点号,
22     for(i=1; i<n; i++)//生成n-1条边
23     {
24         mm=inf;
25         for(j=1; j<=n; j++)
26         {
27             if(dis[j]<mm&&!vis[j])
28             {
29                 mm=dis[j];
30                 pos=j;
31             }
32         }
33         if(mm==inf)//在这里跳出循环说明有路不通。
34         {
35             d=1;
36             break;
37         }
38         vis[pos]=1;//标记找到的节点
39         sum+=mm;//加上最小权值
40         for(j=1; j<=n; j++)//更新dis数组,
41         {
42             if(!vis[j] && mmp[pos][j]<dis[j] )
43                 dis[j] = mmp[pos][j] ;
44             //如果这个点没有被访问更新这点到生成树的距离,
45         }
46     }
47     if(d)
48         cout<<-1<<endl;
49     else
50         cout<<sum<<endl;
51  
52 }
53 int main()
54 {
55     int i,j;
56     while(cin>>n>>m)
57     {
58         for(i=1; i<=n; i++)
59             for(j=1; j<=n; j++)
60                 i==j?mmp[i][j]=0:mmp[i][j]=inf;
61                 while(m--)
62                 {
63                 int a,b,c;
64                 cin>>a>>b>>c;
65                 if(mmp[a][b]>c)
66                 mmp[a][b]=mmp[b][a]=c;
67                 }
68         prim();
69     }
70     return 0;
71 }
72  

n a^o7 !

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description

 

All brave and intelligent fighters, next you will step into a distinctive battleground which is full of sweet and happiness. If you want to win the battle, you must do warm-up according to my instructions, which can make you in the best state preparing to fight. Now please relax yourself and enjoy the good moment. Before you raise your sharp sword to the enemy who guards the battleground, please allow me to tell you a true and romantic story about a samurai like you.  Samurai hh fell in love with girl ss, who is charming and demure. He realized the truth that he must spend his remaining life with ss, and resolved to pursue the hard-won affection. One day hh wrote a letter to ss, when she opens the letter with excitement her mind was in tangle. She found herself completely not to figure out the meaning about the letter, which said that "n 55!w ! pue n a^o7 ! n paau !". ss also immersed herself in guessing the meaning of that letter for a long time because of her adore to hh. Finally she called hh to ask the meaning of the letter. On the other side of the phone, hh was too nervous to say. Gradually he calmed down himself and told ss to reverse the letter and read it. Then on both ends of the phone comes the voice at the same time "i need u i love u and i miss u". ss wants to tell each of you however you are Brave And Skilled, you shouldn't forget to express your loyal love and romantic feelings to your prince or princess. Now the horn sounds for battle,do it by the following input and output. I think each of you will get an "Accepted" in this battle with pleasant mood.

Input

Input contains an integer T in the first line, and then T lines follow .Each line contains a message (only contain 'n5!wpuea^o7!' and  ' '(space)), the message's length is no more than 100.

Output

Output the case number and the message. (As shown in the sample output)

Example Input

2n 55!w ! pue n a^o7 ! n paau !n5!wpuea^o7

Example Output

Case 1: i need u i love u and i miss uCase 2: loveandmisu


001 #include<bits/stdc++.h>
002 using namespace std;
003 char a[20]="n5!wpuea^o7! ";
004 char b[20]="usimdnaevoli ";
005 int main()
006 {
007     char t[150];
008     for(int i = 0; i < 13; i++)
009         t[a[i]] = b[i];
010     int T;
011     scanf("%d\n", &T);
012     for(int i = 1; i <= T; i++)
013     {
014         char c[110];
015         gets(c);
016         printf("Case %d: ", i);
017         for(int j = strlen(c)-1; j >= 0; j--)
018             printf("%c", t[c[j]]);
019         if(i != T)
020             printf("\n");
021     }
022     return 0;
023 }
024 /*#include <iostream>
025 #include <bits/stdc++.h>
026 using namespace std;
027  
028 char a[110], b[110];
029 int main()
030 {
031     int t, l;
032     scanf("%d\n", &t);
033     int k=0;
034     while(t--)
035     {
036         memset(b, 0, sizeof(b));
037         gets(a);
038         l = strlen(a);
039         int j=0;
040         for(int i = l-1; i >= 0; i--)
041         {
042             if(a[i] == '!')
043             {
044                 b[j++] = 'i';
045             }
046             else if(a[i] == 'u')
047             {
048                 b[j++] = 'n';
049             }
050             else if(a[i] == 'a')
051             {
052                 b[j++] = 'e';
053             }
054             else if(a[i] == 'p')
055             {
056                 b[j++] = 'd';
057             }
058             else if(a[i] == 'n')
059             {
060                 b[j++] = 'u';
061             }
062             else if(a[i] == '7')
063             {
064                 b[j++] = 'l';
065             }
066             else if(a[i] == '^')
067             {
068                 b[j++] = 'v';
069             }
070             else if(a[i] == 'e')
071             {
072                 b[j++] = 'a';
073             }
074             else if(a[i] == 'w')
075             {
076                 b[j++] = 'm';
077             }
078             else if(a[i] == '5')
079             {
080                 b[j++] = 's';
081             }
082             else
083             {
084                 b[j++] = a[i];
085             }
086         }
087         k++;
088         printf("Case %d: %s\n", k, b);
089     }
090     return 0;
091 }
092 */





无向图存在欧拉回路的充要条件
一个无向图存在欧拉回路,当且仅当该图所有顶点度数都为偶数,且该图是连通图。
有向图存在欧拉回路的充要条件
一个有向图存在欧拉回路,所有顶点的入度等于出度且该图是连通图。
混合图存在欧拉回路条件
要判断一个混合图G(V,E)(既有有向边又有无向边)是欧拉图,方法如下:
假设有一张图有向图G',在不论方向的情况下它与G同构。并且G'包含了G的所有有向边。那么如果存在一个图G'使得G'存在欧拉回路,那么G就存在欧拉回路。

其思路就将混合图转换成有向图判断。实现的时候,我们使用网络流的模型。现任意构造一个G'。用Ii表示第i个点的入度,Oi表示第i个点的出度。如果存在一个点k,|Ok-Ik|mod 2=1,那么G不存在欧拉回路。接下来则对于所有Ii>Oi的点从源点连到i一条容量为(Ii-Oi)/2的边,对于所有Ii<Oi的点从i连到汇点一条容量为(Oi-Ii)/2的边。如果对于节点U和V,无向边(U,V)∈E,那么U和V之间互相建立容量为无限大的边。如果此网络的最大流等于∑|Ii-Oi|/2,那么就存在欧拉回路。

数据结构实验之图论八:欧拉回路

Time Limit: 1000MS Memory limit: 65536K

题目描述

在哥尼斯堡的一个公园里,有七座桥将普雷格尔河中两个岛及岛与河岸连接起来。



能否走过这样的七座桥,并且每桥只走一次?瑞士数学家欧拉最终解决了这个问题并由此创立了拓扑学。欧拉通过对七桥问题的研究,不仅圆满地回答了哥尼斯堡七桥问题,并证明了更为广泛的有关一笔画的三条结论,人们通常称之为欧拉定理。对于一个连通图,通常把从某结点出发一笔画成所经过的路线叫做欧拉路。人们又通常把一笔画成回到出发点的欧拉路叫做欧拉回路。具有欧拉回路的图叫做欧拉图。

你的任务是:对于给定的一组无向图数据,判断其是否成其为欧拉图?

输入

连续T组数据输入,每组数据第一行给出两个正整数,分别表示结点数目N(1 < N <= 1000)和边数M;随后M行对应M条边,每行给出两个正整数,分别表示该边连通的两个结点的编号,结点从1~N编号。 

输出

若为欧拉图输出1,否则输出0。

示例输入

1
6 10
1 2
2 3
3 1
4 5
5 6
6 4
1 4
1 6
3 4
3 6

示例输出

1

提示

如果无向图连通并且所有结点的度都是偶数,则存在欧拉回路,否则不存在。 

来源

xam

示例程序

 解法一:(并查集)
  1. #include<stdio.h>  
  2. #include<string.h>  
  3. int d[1010],f[1010];  
  4. int t,n,m;  
  5. int find(int x)  
  6. {  
  7.     if(x!=f[x])  
  8.     f[x]=find(f[x]);  
  9.     return f[x];  
  10. }  
  11. void check(int x,int y)  
  12. {  
  13.     int fx=find(x);  
  14.     int fy=find(y);  
  15.     if(fx!=fy)  
  16.         f[fx]=fy;  
  17. }  
  18. int solve()  
  19. {  
  20.     int cnt=0;  
  21.     for(int i=1; i<=n; ++i)  
  22.         {  
  23.         if(f[i]==i)  
  24.         cnt++;  
  25.     }  
  26.     if(cnt!=1)  
  27.     return 0;  
  28.     for(int i=1;i<=n;++i)  
  29.     {  
  30.         if(d[i]%2==1)  
  31.         return 0;  
  32.     }  
  33.     return 1;  
  34. }  
  35. int main()  
  36. {  
  37.     int t;  
  38.     scanf("%d",&t);  
  39.     while(t--)  
  40.         {  
  41.             scanf("%d%d",&n,&m);  
  42.         for(int i=1; i<=n; ++i)  
  43.         {  
  44.             f[i]=i;  
  45.             d[i]=0;  
  46.             }  
  47.         int u,v;  
  48.         for(int i=0; i<m; ++i)  
  49.         {  
  50.             scanf("%d%d",&u,&v);  
  51.     check(u,v);  
  52.     d[u]++;  
  53.     d[v]++;  
  54.         }  
  55.         if(solve())  
  56.             printf("1\n");  
  57.         else  
  58.         printf("0\n");  
  59.     }  
  60.     return 0;  
  61. }  
解法二:(DFS)
  1. #include<stdio.h>  
  2. #include<string.h>  
  3. int map[1010][1010],visited[10100],sum,d[2000],n;  
  4. void DFS(int x)  
  5. {  
  6.     int i;  
  7.     visited[x]=1;  
  8.     sum++;  
  9.     for(i=1;i<=n;i++)  
  10.         if(visited[i]==0&&map[x][i])  
  11.         DFS(i);  
  12. }  
  13. int main()  
  14. {  
  15.     int i,j,m,k,t,l1,l2;  
  16.     scanf("%d",&t);  
  17.     while(t--)  
  18.     {  
  19.         sum=0;  
  20.         memset(map,0,sizeof(map));  
  21.         memset(visited,0,sizeof(visited));  
  22.         memset(d,0,sizeof(d));  
  23.         scanf("%d %d",&n,&m);  
  24.         for(i=0;i<m;i++)  
  25.         {  
  26.             scanf("%d %d",&l1,&l2);  
  27.             map[l1][l2]=1;  
  28.             map[l2][l1]=1;  
  29.             d[l1]++;  
  30.             d[l2]++;  
  31.         }  
  32.         DFS(l1);  
  33.         for(i=1;i<=n;i++)  
  34.             if(d[i]%2==1)  
  35.             break;  
  36.         if(i==n+1&&sum==n)  
  37.             printf("1\n");  
  38.         else  
  39.             printf("0\n");  
  40.     }  
  41. }  

解法三:(BFS)
  1. #include<stdio.h>  
  2. #include<string.h>  
  3. int map[1010][1010],visited[10100],sum,d[2000],d1[2000],n;  
  4. void BFS(int s)  
  5. {  
  6.    int out=0,in=0,v,i;  
  7.    visited[s]=1;  
  8.    sum++;  
  9.    d1[in++]=s;  
  10.    while(out<in)  
  11.    {  
  12.        v=d1[out++];  
  13.        for(i=1;i<=n;i++)  
  14.         if(visited[i]==0&&map[v][i])  
  15.        {  
  16.            visited[i]=1;  
  17.            sum++;  
  18.            d1[in++]=i;  
  19.        }  
  20.    }  
  21. }  
  22. int main()  
  23. {  
  24.     int i,j,m,k,t,l1,l2;  
  25.     scanf("%d",&t);  
  26.     while(t--)  
  27.     {  
  28.         sum=0;  
  29.         memset(map,0,sizeof(map));  
  30.         memset(visited,0,sizeof(visited));  
  31.         memset(d,0,sizeof(d));  
  32.         scanf("%d %d",&n,&m);  
  33.         for(i=0;i<m;i++)  
  34.         {  
  35.             scanf("%d %d",&l1,&l2);  
  36.             map[l1][l2]=1;  
  37.             map[l2][l1]=1;  
  38.             d[l1]++;  
  39.             d[l2]++;  
  40.     }  
  41.         BFS(1);  
  42.         for(i=1;i<=n;i++)  
  43.             if(d[i]%2==1)  
  44.             break;  
  45.         if(i==n+1&&sum==n)  
  46.             printf("1\n");  
  47.         else  
  48.             printf("0\n");  
  49.     }  


图中点的度是指这个点相连的边的数目
一般来说,图可分为有向图和无向图。有向图的所有边都有方向,即确定了顶点到顶点的一个指向;而无向图的所有边都是双向的,即无向边所连接的两个顶点可以互相到达。在一些问题中,可以把无向图当作所有边都是正向和负向的两条有向边组成。顶点的度是指和该顶点相连的边的条数。特别是对于有向图来说,顶点的出边条数称为该顶点的出度,顶点的入边条数称为该项点的入度。

完美网络

Time Limit: 1000MS   Memory Limit: 65536KB

Problem Description

完美网络是连通网络的基础上要求去掉网络上任意一条线路,网络仍然是连通网络。求一个连通网络要至少增加多少条边可以成为完美网络。

Input

第一行输入一个数T代表测试数据个数(T<=20)。每个测试数据第一行2个数n,m 分别代表网络基站数和基站间线路数。基站的序号为从1到n。接下来m行两个数代表x,y 代表基站x,y间有一条线路。
( 0 < n < m < 10000)

Output

对于每个样例输出最少增加多少线路可以成为完美网络。每行输出一个结果。

Example Input

2
3 1
1 2
3 2
1 2
2 3

Example Output

2
1 


      
      
01 #include<bits/stdc++.h>
02 using namespace std;
03 int dg[10000100];//点的度。
04 int main()
05 {
06     int t,n,m,i,x,y;
07     cin>>t;
08     while(t--)
09     {
10         priority_queue<int, vector<int>, greater<int> >que;
11         memset(dg,0,sizeof(dg));
12         cin>>n>>m;
13         for(i=0; i<m; i++)
14         {
15             cin>>x>>y;
16             dg[x-1]++;
17             dg[y-1]++;
18         }
19         sort(dg,dg+n);
20         for(i=0; i<n; i++)
21         {
22             if(dg[i]<2)
23                 que.push(dg[i]);
24         }
25         int cnt=0;//需要加的边数。
26         while(que.size()>=2)
27         {
28             int t=0,f=0;
29             t=que.top();
30             que.pop();
31             f=que.top();
32             que.pop();
33             ++t;
34             ++f;
35             cnt++;//取出两点构造一条边,。
36             if(t<2)
37                 que.push(t);
38             if(f<2)
39                 que.push(f);
40         }
41         if(!que.empty())
42             //如果执行完上述while循环后,
43             // 队列还不为空
44 //则这种情况只能说明,队列里还剩下一个元素,
45 //他的度仍小于2.
46             cnt+=1;
47             cout<<cnt<<endl;
48         }
49         return 0;
50     }
51  
52  

数据结构实验之查找三:树的种类统计(stl树的使用。)

Time Limit: 400MS   Memory Limit: 65536KB

Problem Description

随着卫星成像技术的应用,自然资源研究机构可以识别每一个棵树的种类。请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比。

Input

输入一组测试数据。数据的第1行给出一个正整数N (n <= 100000),N表示树的数量;随后N行,每行给出卫星观测到的一棵树的种类名称,树的名称是一个不超过20个字符的字符串,字符串由英文字母和空格组成,不区分大小写。

Output

按字典序输出各种树的种类名称和它占的百分比,中间以空格间隔,小数点后保留两位小数。

Example Input

2
This is an Appletree
this is an appletree

Example Output

this is an appletree 100.00%

01 #include <bits/stdc++.h>
02 using namespace std;
03 int main()
04 {
05     int n,i,j,tmp;
06     cin>>n;
07     map<string,int>mmp;
08     map<string,int>::iterator cnt;
09     char s[10000],str[10000];
10     getchar();
11     for(j=0; j<n; j++)
12     {
13         memset( str, 0, sizeof(str) );
14         gets(s);
15         tmp=0;
16         for(i=0; i<strlen(s); i++)
17             if(s[i]>='A'&&s[i]<='Z')
18                 str[tmp++]=s[i]+32;
19             else
20                 str[tmp++]=s[i];
21         mmp[str]++;
22     }
23     double x;
24     for(cnt=mmp.begin(); cnt!=mmp.end(); cnt++)
25     {
26         x=(cnt->second*100.0)/(n*1.0);
27         printf("%s %.2lf",cnt->first.c_str(),x);
28         printf("%c",'%');
29         printf("\n");
30     }
31     return 0;
32 }
33  

数据结构实验之查找七:线性之哈希表

Time Limit: 1000MS   Memory Limit: 65536KB

Problem Description

根据给定的一系列整数关键字和素数p,用除留余数法定义hash函数H(Key)=Key%p,将关键字映射到长度为p的哈希表中,用线性探测法解决冲突。重复关键字放在hash表中的同一位置。

Input

连续输入多组数据,每组输入数据第一行为两个正整数N(N <= 1500)和p(p >= N的最小素数),N是关键字总数,p是hash表长度,第2行给出N个正整数关键字,数字间以空格间隔。

Output

输出每个关键字在hash表中的位置,以空格间隔。注意最后一个数字后面不要有空格。

Example Input

5 5
21 21 21 21 21
4 5
24 15 61 88
4 5
24 39 61 15
5 5
24 39 61 15 39

Example Output

1 1 1 1 1
4 0 1 3
4 0 1 2
4 0 1 2 0
01 #include <iostream>
02 #include <cstdio>
03 using namespace std;
04 int n,p;
05 int insert(int *f,int k)
06 {
07     int h=k%p;
08     int i=1,j=h;
09     while(f[j]!=0&&f[j]!=k)
10         j=(h+i++)%p;
11     f[j]=k;
12     return j;
13 }
14 int main()
15 {
16     while(scanf("%d %d",&n,&p)!=EOF)
17     {
18         int hash[2011]={0};
19         int i;
20         for(i=0;i<n;i++)
21         {
22             int k;
23             scanf("%d",&k);
24             printf("%d%c",insert(hash,k),i==n-1?'\n':' ');
25         }
26     }
27     return 0;
28 }
29  


猜你喜欢

转载自blog.csdn.net/beposit/article/details/79239042
o
今日推荐