12/3

1144 The Missing Number (20 分)

Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10^​5 ). Then N integers are given in the next line, separated by spaces. All the numbers are in the range of int.

Output Specification:

Print in a line the smallest positive integer that is missing from the input list.

Sample Input:

10
5 -25 9 6 1 3 4 2 5 17

Sample Output:

7

数据里头添加范围包括 N (≤10^​5 )。

1140 Look-and-say Sequence (20 分)

Look-and-say sequence is a sequence of integers as the following:
D, D1, D111, D113, D11231, D112213111, …
where D is in [0, 9] except 1. The (n+1)st number is a kind of description of the nth number. For example, the 2nd number means that there is one D in the 1st number, and hence it is D1; the 2nd number consists of one D (corresponding to D1) and one 1 (corresponding to 11), therefore the 3rd number is D111; or since the 4th number is D113, it consists of one D, two 1’s, and one 3, so the next number must be D11231. This definition works for D = 1 as well. Now you are supposed to calculate the Nth number in a look-and-say sequence of a given digit D.

Input Specification:

Each input file contains one test case, which gives D (in [0, 9]) and a positive integer N (≤ 40), separated by a space.

Output Specification:

Print in a line the Nth number in a look-and-say sequence of D.

Sample Input:

1 8

Sample Output:

1123123111

末尾的字符串不要忘记加

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int MAX=10005;
const int INF=0x3f3f3f3f;
string a;
int n;
int main()
{
    cin>>a>>n;
    while(n>1)
    {
        string ans;
        char c=a[0];
        int cnt=0;
        for(int i=0;i<a.length();i++)
        {
            if(a[i]==c)
                cnt++;
            else
            {
                ans+=c;
                ans+=(cnt+'0');
                c=a[i];
                cnt=1;
            }
        }
        if(cnt>0)
        {
            ans+=c;
            ans+=(cnt+'0');
        }
        a=ans;
        n--;
    }
    cout<<a<<endl;
    return 0;
}

1141 PAT Ranking of Institutions (25 分)

After each PAT, the PAT Center will announce the ranking of institutions based on their students’ performances. Now you are asked to generate the ranklist.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10^​5​​ ), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:
ID Score School
where ID is a string of 6 characters with the first one representing the test level: B stands for the basic level, A the advanced level and T the top level; Score is an integer in [0, 100]; and School is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that ID is unique for each testee.

Output Specification:

For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:
Rank School TWS Ns
where Rank is the rank (start from 1) of the institution; School is the institution code (all in lower case); ; TWS is the total weighted score which is defined to be the integer part of ScoreB/1.5 + ScoreA + ScoreT*1.5, where ScoreX is the total score of the testees belong to this institution on level X; and Ns is the total number of testees who belong to this institution.
The institutions are ranked according to their TWS. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns. If there is still a tie, they shall be printed in alphabetical order of their codes.

Sample Input:

10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu

Sample Output:

5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2

19分,自闭了2处错误,一处超时,/(ㄒoㄒ)/~~,待更

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int MAX=100005;
const int INF=0x3f3f3f3f;
int n;
struct node
{
    string id;
    int score;
    int sumscore;
    int number;
    string name;
}num[MAX],pm[MAX];
map<string,int>book;
map<string,int>schtoA,schtoB,schtoT,sum;
set<string>st;
string change(string x)
{
    for(int i=0;i<x.length();i++)
    {
        if(x[i]>='A'&&x[i]<='Z')
        {
            x[i]=x[i]-'A'+'a';
        }
    }
    return x;
}
bool cmp(node a,node b)
{
    if(a.sumscore!=b.sumscore)
        return a.sumscore>b.sumscore;
    else if(a.number!=b.number)
        return a.number<b.number;
    else
        return a.name<b.name;

}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        cin>>num[i].id>>num[i].score>>num[i].name;
        book[change(num[i].name)]++;
        st.insert(change(num[i].name));
        if(num[i].id[0]=='A')
        {
            schtoA[change(num[i].name)]+=num[i].score;
        }
        else if(num[i].id[0]=='B')
        {
            schtoB[change(num[i].name)]+=num[i].score;
        }
        else if(num[i].id[0]=='T')
        {
            schtoT[change(num[i].name)]+=num[i].score;
        }
    }
    int t=0;
    for(set<string>::iterator it=st.begin();it!=st.end();it++)
    {
        sum[*it]=schtoB[*it]/1.5+schtoA[*it]+schtoT[*it]*1.5;
        pm[t].name=*it;
        pm[t].sumscore=sum[*it];
        pm[t].number=book[*it];
        t++;
    }
    sort(pm,pm+t,cmp);
    printf("%d\n",t);
    for(int k=0;k<t;k++)
    {
        if(pm[k].sumscore==pm[k-1].sumscore)
        {
            cout<<k<<" "<<pm[k].name<<" "<<pm[k].sumscore<<" "<<pm[k].number<<endl;
        }
        else
        cout<<k+1<<" "<<pm[k].name<<" "<<pm[k].sumscore<<" "<<pm[k].number<<endl;
    }

    return 0;
}

1142 Maximal Clique (25 分)

A clique is a subset of vertices of an undirected graph such that every two distinct vertices in the clique are adjacent. A maximal clique is a clique that cannot be extended by including one more adjacent vertex. (Quoted from https://en.wikipedia.org/wiki/Clique_(graph_theory))
Now it is your job to judge if a given subset of vertices can form a maximal clique.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers Nv (≤ 200), the number of vertices in the graph, and Ne, the number of undirected edges. Then Ne lines follow, each gives a pair of vertices of an edge. The vertices are numbered from 1 to Nv.
After the graph, there is another positive integer M (≤ 100). Then M lines of query follow, each first gives a positive number K (≤ Nv), then followed by a sequence of K distinct vertices. All the numbers in a line are separated by a space.

Output Specification:

For each of the M queries, print in a line Yes if the given subset of vertices can form a maximal clique; or if it is a clique but not a maximal clique, print Not Maximal; or if it is not a clique at all, print Not a Clique.

Sample Input:

8 10
5 6
7 8
6 4
3 6
4 5
2 3
8 2
2 7
5 3
3 4
6
4 5 4 3 6
3 2 8 7
2 2 3
1 1
3 4 3 6
3 3 2 1

Sample Output:

Yes
Yes
Yes
Yes
Not Maximal
Not a Clique

每个点常规遍历过去就好了

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int MAX=1005;
const int INF=0x3f3f3f3f;
int n,m,k,num,t[MAX];
int G[MAX][MAX];
int book[MAX];
int main()
{
   memset(G,0,sizeof(G));
   scanf("%d%d",&n,&m);
   for(int i=0;i<m;i++)
   {
       int x,y;
       scanf("%d%d",&x,&y);
       G[x][y]=G[y][x]=1;
   }
   scanf("%d",&k);
   for(int i=0;i<k;i++)
   {
       memset(book,0,sizeof(book));
       scanf("%d",&num);
       int flag=1,flag2=1;
       for(int j=0;j<num;j++)
       {
           scanf("%d",&t[j]);
           book[t[j]]=1;
       }
       for(int j=0;j<num;j++)
       {
           if(flag==0)
            break;
           for(int k=1+j;k<num;k++)
           {
               if(G[t[j]][t[k]]==0)
               {
                   flag=0;
                   printf("Not a Clique\n");
                   break;
               }
           }
       }
       if(flag==0)
        continue;
       for(int j=1;j<=n;j++)
       {
           if(book[j]==0)
           {
               for(int k=0;k<num;k++)
               {
                if(G[t[k]][j]==0)
                    break;
                   if(k==num-1)
                       flag2=0;
               }
            }
            if(flag2==0)
            {
                printf("Not Maximal\n");
                break;
            }
       }
       if(flag2==1)
        printf("Yes\n");
   }
   return 0;
}

1145 Hashing - Average Search Time (25 分)

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether or not the key is in the table). The hash function is defined to be H(key)=key%TSize where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.
Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

Input Specification:

Each input file contains one test case. For each case, the first line contains 3 positive numbers: MSize, N, and M, which are the user-defined table size, the number of input numbers, and the number of keys to be found, respectively. All the three numbers are no more than 10^​4
​​ . Then N distinct positive integers are given in the next line, followed by M positive integer keys in the next line. All the numbers in a line are separated by a space and are no more than 10^​5
​​ .

Output Specification:

For each test case, in case it is impossible to insert some number, print in a line X cannot be inserted. where X is the input number. Finally print in a line the average search time for all the M keys, accurate up to 1 decimal place.

Sample Input:

4 5 4
10 6 4 15 11
11 4 15 2

Sample Output:

15 cannot be inserted.
2.8

哈希散列,二次方探测法,
哈希函数构造方法:H(key)=key%TSize
处理冲突方法:Hi = (H(key) + di) % TSize
其中di为 1 * 1 , -1 * 1 , 2 * 2 , -2 * 2 , ··· k * k , -k * k (k <= MSize-1)
positive increments only所以是正向探测

对于无法插入的情况,如果k <= Msize-1 内都没有找到合适位置,则插入失败。
在查询时,执行和插入一样的操作,如果定位到的元素和当前元素相同,说明找到了;如果定位到的位置是空的,说明当前查询的元素不在哈希表中,两种情况都直接退出;如果k <= Msize-1 范围内没有找到,并且不确定是否有该元素(没明白。。。),则说明该元素无法插入,查询总数要+1。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std;
const int MAX=100010;
const int INF=0x3f3f3f3f;
int msize,n,m;
int book[MAX];
bool prime(int x)
{
    if(x<=1)
        return false;
    for(int i=2;i<=(int)sqrt(1.0*x);i++)
    {
        if(x%i==0)
        {
            return false;
        }
    }
    return true;
}
int hkey(int key,int tsize)
{
    return key%tsize;
}
int main()
{
    memset(book,0,sizeof(book));
    scanf("%d%d%d",&msize,&n,&m);
    while(prime(msize)==false)
        msize++;
    int t;
    for(int i=0;i<n;i++)
    {
        scanf("%d",&t);
        int flag=1;
        for(int j=0;j<msize;j++)
        {
            int d=j*j;
            int tid=(hkey(t,msize)+d)%msize;
            if(book[tid]==0)
            {
                book[tid]=t;
                flag=0;
                break;
            }
        }
        if(flag==1)
            printf("%d cannot be inserted.\n",t);
    }
    int tol=0;
    for(int i=0;i<m;i++)
    {
        int flag2=0;
        scanf("%d",&t);
        for(int j=0;j<msize;j++)
        {
            int d=j*j;
            int tid=(hkey(t,msize)+d)%msize;
            tol++;
            if(book[tid]==t||book[tid]==0)
            {
                flag2=1;
                break;
            }
        }
        if(flag2==0)
            tol++;
    }
    printf("%.1f\n",tol*1.0/m);
   return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值