2018PKU软件工程夏令营机试题目选

B:n-gram串频统计

总时间限制: 1000ms 内存限制: 65536kB
描述
在文本分析中常用到n-gram串频统计方法,即,统计相邻的n个单元(如单词、汉字、或者字符)在整个文本中出现的频率。假设有一个字符串,请以字符为单位,按n-gram方法统计每个长度为 n 的子串出现的频度,并输出最高频度以及频度最高的子串。所给的字符串只包含大小写字母,长度不多于500个字符,且 1 < n < 5。

如果有多个子串频度最高,则根据其在序列中第一次出现的次序依次输出,每行输出一个,如果最高频度不大于1,则输出NO。

输入
第一行为n;
第二行为字符串。
输出
输出最高频度以及频度最高的所有子串。若最高频度不大于1,只输出一行NO。
样例输入
3
abcdefabcd
样例输出
2
abc
bcd
提示
样例中,所有的3-gram是:abc,bcd,cde,def,efa,fab,abc,bcd。最后面的cd不足以形成3-gram,则不考虑。这样,abc 和 bcd 都出现了2次,其余的只出现了1次。

主要思路:
一个数组,存字符串,另一个数组,存字符串长度。
遍历可能的所有字符串,1.此字符串出现过没有,出现过,次数+1。2.此字符串没有出现过,新增。
最后先得到次数最多的字符串,然后找和其相同长度字符串,用queue存取。

代码

//============================================================================
// Name        : ex1.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;



int main() {
    int m;
    cin>>m;

    string s;
    cin>>s;

    string sary[505];//存字符串
    int snum[505];//存词频

    fill(sary,sary+505,"9");
//  cout< out;
    for(int j=0;jsnum[maxindex]){
            maxindex=j;
//          cout<<"sd"<1){
            cout<

C:垂直直方图

总时间限制: 1000ms 内存限制: 65536kB
描述
输入4行全部由大写字母组成的文本,输出一个垂直直方图,给出每个字符出现的次数。注意:只用输出字符的出现次数,不用输出空白字符,数字或者标点符号的输出次数。

输入
输入包括4行由大写字母组成的文本,每行上字符的数目不超过80个。
输出
输出包括若干行。其中最后一行给出26个大写英文字母,这些字母之间用一个空格隔开。前面的几行包括空格和星号,每个字母出现几次,就在这个字母的上方输出一个星号。注意:输出的第一行不能是空行。

样例输入
THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG.
THIS IS AN EXAMPLE TO TEST FOR YOUR
HISTOGRAM PROGRAM.
HELLO!
样例输出
                            *
                            *
        *                   *
        *                   *     *   *
        *                   *     *   *
*       *     *             *     *   *
*       *     * *     * *   *     * * *
*       *   * * *     * *   * *   * * * *
*     * * * * * *     * * * * *   * * * *     * *
* * * * * * * * * * * * * * * * * * * * * * * * * *
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

思路:
这个很简单,如果卡的话可能是码代码时数值输入的原因。

代码

//============================================================================
// Name        : ex2.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

int main() {
    string in[4];
    for(int i=0;i<4;i++){
        getline(cin,in[i]);
    }
    int alp[26];
    memset(alp,0,sizeof(alp));
    for(int i=0;i<4;i++){
        for(int j=0;j='A'&&in[i][j]<='Z'){
                alp[in[i][j]-65]++;
            }
        }
    }

    int maxindex=0;

        for(int j=1;j<26;j++){
            if(alp[j]>alp[maxindex]){
                maxindex=j;
            }
        }
    char res[alp[maxindex]+1][26];


    for(int i=0;i<26;i++){
        int count=alp[i];
        for(int j=alp[maxindex];j>=0;j--){
            if(j==alp[maxindex]){
                res[j][i]=i+65;
            }else if(count>0){
                res[j][i]='*';
                count--;
            }else{
                res[j][i]=' ';
            }
        }
    }
    for(int i=0;i

G:Networking

【后来发现这个代码可以直接用三维数组进行存图,运算,不用矩阵,应该空间复杂度小很多,可以用vector,也更方便,不过不知道为什么后来写的很短的代码AC不了,等AC再放上来做补充。】

软工夏令营上机题目G & 百练1287

题目描述

  • 总时间限制:

    1000ms

  • 内存限制:

    65536kB

  • 描述

    You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area. Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.

  • 输入

    The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line. The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.

  • 输出

    For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

样例输入

1 0

2 3
1 2 37
2 1 17
1 2 68

3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32

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

0

样例输出

0
17
16
26

主要思路

1.构造无向图。(这里利用矩阵的方法)

2.找出无向图中的最小生成树。

这里使用Kruskal算法,利用并查集写。

(就是做这道题时发现我对Kruskal的理解有一丢丢的失误= =,可见还是码出来最重要)

//============================================================================
// Name        : ex3.cpp
// Author      : yongbo
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include
#include
#include 
#include 
#include 
#include 
#include 
using namespace std;

int point[55];
int findfather(int a){
    if(point[a]!=a){
        return findfather(point[a]);
    }else{
        return a;
    }
}

void unionab(int a,int b){
    if(findfather(a)!=findfather(b)){
        point[a]=b;
    }
}

void unionab(int a,int b){
    //if(findfather(a)!=find) 如果两者不在同一阵营
    int q=findfather(a);
    int p=findfather(b);
    if(p!=q)
        point[p]=q;
}

bool issame(int a,int b){
    if(findfather(a)==findfather(b))
        return true;
    else
        return false;
}

int main(){

    int m;
    queue answer;
    scanf("%d",&m);
    while(m!=0){
        int n;
        scanf("%d",&n);
        if(n==0){
            printf("%d",0);
            printf("%s","\n");
//          string s;
//          getline(cin,s);
            scanf("%d",&m);
            continue;

        }else{

            for(int i=0;i<55;i++){
                point[i]=i;
            }
            int line[n][3];
            for(int i=0;ilen){
                    graph[x][y]=len;
                    graph[y][x]=len;
                }

            }
            int edge=0;
            for(int i=0;i

你可能感兴趣的:(2018PKU软件工程夏令营机试题目选)