Data structure week 15: (find the kth largest number + find the smallest common element of 3 arrays + find the smallest element of a circular sequence array + Crazy Search)

Find the kth largest number

[Problem description] Find the kth largest number among n numbers
[Input form] The first line is nk, and the second line is n numbers, all separated by spaces
[Output form] The kth largest number
[Sample input]
10 3
18 21 11 26 12 2 9 33 43 28

【Sample output】

28

[Scoring criteria] A method with a time complexity greater than or equal to O(k n) gets half a score, and a method with a time complexity less than or equal to O(n log2k) gets a full score.

【hint】

  1. Analyze the advantages and disadvantages of various sorting or search algorithms, analyze the time complexity of solving specific problems, and then find out more efficient algorithms.

  2. The values ​​of n and k are different, and the efficiency of different algorithms will also be affected. For example, when n=10 and k=9, you can find the second smallest number.

#include<iostream>
using namespace std;

int QSort(int a[], int left, int right, int rk)
{
    
    
    int low = left;
    int high = right;
    int flag = a[low];
    while(low < high)
    {
    
    
        while(a[high] <= flag && low < high)
        {
    
    
            high --;
        }
        a[low] = a[high];
        while(a[low] >= flag && low < high)
        {
    
    
            low ++;
        }
        a[high] = a[low];
    }
    a[low] = flag;
    if(low == rk - 1)
        return a[low];
    else if(low > rk - 1)
        return QSort(a, left, low - 1, rk);
    else
        return QSort(a, low + 1, right, rk - low);
}
int main()
{
    
    
    int n, k;
    cin>>n>>k;
    int i = n;
    int j = -1;
    int a[n];
    while(i --)
    {
    
    
        cin>>a[++ j];
    }
    cout<<QSort(a, 0, n - 1, k);
}

Find the smallest common element of 3 arrays

[Problem description] Find the smallest common element of 3 arrays
[Input form] Three arrays, all end with 0 for input
[Output form] The smallest common element
[Sample input]

1 3 5 7 8 9 0

2 4 6 8 10 12 14 16 18 0

-1 3 8 16 18 19 20 168 198 0
【Sample output】

8

#include<iostream>
using namespace std;
int a[1000], b[1000], c[1000];
int main()
{
    
    
    int i = 0;
    int num = 0;
    for(i = 0; ; i ++)
    {
    
    
        cin>>num;
        if(num == 0) break;
        a[i] = num; //i取到数组最后一个下标
    }
    int alen = i;
    for(i = 0; ; i ++)
    {
    
    
        cin>>num;
        if(num == 0) break;
        b[i] = num; //i取到数组最后一个下标
    }
    int blen = i;
    for(i = 0; ; i ++)
    {
    
    
        cin>>num;
        if(num == 0) break;
        c[i] = num; //i取到数组最后一个下标
    }
    int clen = i;
    int pa = 0, pb = 0, pc = 0;
    while(pa <= alen && pb <= blen && pc <= clen)
    {
    
    
        if(a[pa] == b[pb] && b[pb] == c[pc])
        {
    
    
            cout<<a[pa];
            break;
        }
        while(a[pa] < b[pb] && pa <= alen)
        {
    
    
            pa ++;
        }
        while(b[pb] < a[pa] && pb <= blen)
        {
    
    
            pb ++;
        }
        while(c[pc] < b[pb] && pc <= clen)
        {
    
    
            pc ++;
        }
    }
    return 0;
}

Find the smallest element of a circular sequential array

[Problem description] A set of sequential data arranged in a loop is stored in a one-dimensional array, and the smallest element is found and output.
[Input form] A set of data, input ending with 0
[Output form] Minimum element
[Sample input] 7 9 11 1 3 5 0
[Sample output] 1

#include<iostream>
#define N 100
using namespace std;

int FindMin(int a[], int low, int high)
{
    
    
    int mid = (low + high) / 2;
    if(a[low] < a[high])
        return a[low];
    else
    {
    
    
        if(a[low] < a[mid])
        {
    
    
            return FindMin(a, mid + 1, high);
        }
        else if(a[low] == a[mid]) return a[low];
        else
        {
    
    
            return FindMin(a, low + 1, mid);
        }
    }
}
int main()
{
    
    
    int a[N];
    int i = 0;
    int num = 0;
    for(i = 0; ;i ++)
    {
    
    
        cin>>num;
        if(num == 0) break;
        a[i] = num;
    }
    cout<<FindMin(a, 0, i - 1); //i取不到
    return 0;
}

Crazy Search

[Title source] 1200 – Crazy Search (poj.org) Please go to this link to submit the detection code

Description

Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could be finding a hidden prime number in a given text. Such number could be the number of different substrings of a given size that exist in the text. As you soon will discover, you really need the help of a computer and a good algorithm to solve such a puzzle.
Your task is to write a program that given the size, N, of the substring, the number of different characters that may occur in the text, NC, and the text itself, determines the number of different substrings of size N that appear in the text.

As an example, consider N=3, NC=4 and the text “daababac”. The different substrings of size 3 that can be found in this text are: “daa”; “aab”; “aba”; “bab”; “bac”. Therefore, the answer should be 5.

Input

The first line of input consists of two numbers, N and NC, separated by exactly one space. This is followed by the text where the search takes place. You may assume that the maximum number of substrings formed by the possible set of characters does not exceed 16 Millions.

Output

The program should output just an integer corresponding to the number of different substrings of size N found in the given text.

Sample Input

3 4
daababac
Sample Output

5

#include<iostream>
#include<string>
#include<string.h>
using namespace std;

const int N = 1600000; // 定义16000000为什么不能运行

int main()
{
    
    
    int res = 0;
    string s;
    int sonlen;
    int sysnum; //字符串中可能出现的字符种类数
    cin>>sonlen;
    cin>>sysnum;
    cin>>s;
    int slen = s.length(); //调用string类的类函数
    int i = 0;
    bool Hash[N];
    memset(Hash, 0, sizeof(Hash));
    for(i = 0; i <= slen - sonlen; i ++)
    {
    
    
        string temp = s.substr(i,3); //截取字符串片段
        int pos = 0;
        int j = 0;
        for(j = 0; j < sonlen; j ++)
        {
    
    
            int k = 1;
            int t = int(temp[j]);
            for(k = j + 1; k <= sysnum; k ++)
            {
    
    
                t *= sysnum;
            }
            pos += t;
        }
        if(!Hash[pos])
        {
    
    
            Hash[pos] = 1;
            res ++;
        }
    }
    cout<<res;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_51800570/article/details/129186053