STL title

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/Void_worker/article/details/82819454

1. Title:
small L D is
a time limit: 4000 ms | Memory Limit: 65535 KB
Difficulty: 2
Description
one day TC Marina Marina looking ACM small L play Three killed, but this small L busy miles, do not want to Marina Marina play but Marina Marina fear angry, when small Marina Marina L to a subject like a baffled Marina Marina (very small L D it), there is a number n (0 <n <10) , write a full array 1 to n , then a little embarrassing Marina Marina ,,, Marina Marina clever you help rescue it?
Input
of the first input line of a number N (0 <N <10) , there are N sets of test data indicates. N rows of input following the input of a plurality of sets of data, each set of input data is an integer x (0 <x <10)
outputs
the output of all combined in a particular order.
Particular order: a value for each combination in ascending order, according to the combination between the lexicographic order.
Sample input
2
2
. 3
sample output
12 is
21 is
123
132
213
231
312
321

Code:

#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 10;

int main(){
//    freopen("a.txt", "r", stdin);
    int n, x;
    int l[maxn];
    scanf("%d", &n);
    while(n--){
        scanf("%d", &x);
        for(int i = 0; i < x; ++i){
            l[i] = i + 1;
        }
        do{
            for(int i = 0; i < x; ++i){
                printf("%d", l[i]);
            }
            printf("\n");
        }while(next_permutation(l, l + x));
    }
    return 0;
}

Next larger array generation function will next_permutation alphabetical order given sequence, up until the entire sequence is descending. prev_permutation contrast function, is arranged to generate a smaller given sequence.

Before full alignment, array elements must be sorted in small to large
next_permutation () function is the first parameter: the starting position of the array (i.e., array name)
second parameter: end position of the array (i.e., array name array length +) .

2. seek times (STL map, STL string)
time limit: 1000 ms | memory limit: 65535 KB
Difficulty: 2
describes
the meaning of problems is very simple, and a number n to a string str, interval [i, i + n-1 ] as a new string, i belonging to {0, strlen (str)] If the new string appeared ans ++, for example: acmacm n = 3, then the substring acm cma mac acm, there have been only acm

Seeking ans;

Input
LINE 1: T set of data (T <10)
the LINE 2: n-, n-<= 10, and less than strlen (STR);
the LINE. 3: STR
STR contains only lowercase letters, cut length is less than 10w
output
requirements ans
Sample Input
2
2
AAAAAAA
. 3
acmacm
sample output
. 5
. 1
by
ACM_ Wangya Long

Links:
http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1112

#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <map>
using namespace std;
//map<string, int> mp;

int main(){
//    freopen("a.txt", "r", stdin);
    int t, n;
    int len, ans;
    string s, ss;
    cin >> t;
    while(t--){
        map<string, int> mp;
        ans = 0;
        cin >> n >> s;
        len = s.length();
        for(int i = 0; i <= len - n; ++i){
            ss = s.substr(i, n);
            if(mp[ss]) ans++;
            else mp[ss] = 1;
        }
        cout << ans << endl;
    }
    return 0;
}

3.Registration system
time limit: 1000 ms | memory limit: 65535 KB
Difficulty: 2
Description
A new e-mail service "Berlandesk " is going to be opened in Berland in the near future.

The site administration wants to launch their project as soon as possible, that’s why they

ask you to help. You’re suggested to implement the prototype of site registration system.

The system should work on the following principle.

Each time a new user wants to register, he sends to the system a request with his name.

If such a name does not exist in the system database, it is inserted into the database, and

the user gets the response OK, confirming the successful registration. If the name already

exists in the system database, the system makes up a new user name, sends it to the user

as a prompt and also inserts the prompt into the database. The new name is formed by the

following rule. Numbers, starting with 1, are appended one after another to name (name1,

name2, …), among these numbers the least i is found so that namei does not yet exist in

the database.

输入
The first line contains number n (1 ≤ n ≤ 105). The following n lines contain the requests to the system. Each request is a non-empty line, and consists of not more than 1000 characters, which are all lowercase Latin letters.
输出
Print n lines, which are system responses to the requests: OK in case of successful registration, or a prompt with a new name, if the requested name is already taken.
样例输入
4
abacaba
acaba
abacaba
acab
样例输出
OK
OK
abacaba1
OK

#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <map>
using namespace std;
map<string, int> mp;

int main(){
//    freopen("a.txt", "r", stdin);
    int n;
    string s;
    cin >> n;
    while(n--){
        cin >> s;
        if(mp[s]){
            cout << s << mp[s] << endl;
            mp[s]++;
        }
        else{
            mp[s] = 1;
            cout << "OK" << endl;
        }
    }
    return 0;
}

4.Same binary weight
Time limit: 300 ms | Memory Limit: 65535 KB
Difficulty: 3

description

The binary weight of a positive integer is the number of 1’s in its binary representation.for example,the decmial number 1 has a binary weight of 1,and the decimal number 1717 (which is 11010110101 in binary) has a binary weight of 7.Give a positive integer
N,return the smallest integer greater than N that has the same binary weight as N.N will be between 1 and 1000000000,inclusive,the result is guaranteed to fit in a signed 32-bit interget.

输入The input has multicases and each case contains a integer N.
输出For each case,output the smallest integer greater than N that has the same binary weight as N.
样例输入
1717
4
7
12
555555

Sample output
1718
. 8
. 11
. 17
555 557

Source topcoder
By Luo Kuiyong

Code:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 42;
int nb[maxn], res[maxn];

int main(){
//    freopen("a.txt", "r", stdin);
    int n, cur, ans;
    while(scanf("%d", &n) != EOF){
        cur = 0;
        memset(nb, 0, sizeof(nb));
        memset(res, 0, sizeof(res));
        while(n > 0){
            nb[cur++] = n % 2;
            n /= 2;
        }
        for(int i = 0; i < 32; ++i){
            res[i] = nb[31 - i];
        }
        next_permutation(res, res + 32);
        ans = 0;
        for(int i = 0; i < 32; ++i){
            ans = ans * 2 + res[i];
        }
        printf("%d\n", ans);
    }
    
    return 0;
}

5.Ignatius and the Princess IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K (Java/Others)
Total Submission(s): 44023 Accepted Submission(s): 19419

Problem Description
“OK, you are not too bad, em… But you can never pass the next test.” feng5166 says.

“I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers.” feng5166 says.

“But what is the characteristic of the special integer?” Ignatius asks.

“The integer will appear at least (N+1)/2 times. If you can’t find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha…” feng5166 says.

Can you find the special integer for Ignatius?

Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.

Output
For each test case, you have to output only one line which contains the special number you have found.

Sample Input
5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1

Sample Output
3
5
1

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
map<int, int> mp;

int main(){
//    freopen("a.txt", "r", stdin);
    int n, t;
    while(scanf("%d", &n) != EOF){
        mp.clear();
        for(int i = 0; i < n; ++i){
            scanf("%d", &t);
            if(mp[t]) mp[t]++;
            else mp[t] = 1;
        }
        map<int, int>::iterator it = mp.begin();
        for(it; it != mp.end(); it++){
            if(it->second >= (n + 1) / 2){
                printf("%d\n", it->first);
                break;
            }
        }
    }
    return 0;
}

题目:
1063 Set Similarity (25 分)
Given two sets of integers, the similarity of the sets is defined to be N
​c
​​ /N
​t
​​ ×100%, where N
​c
​​ is the number of distinct common numbers shared by the two sets, and N
​t
​​ is the total number of distinct numbers in the two sets. Your job is to calculate the similarity of any given pair of sets.

Input Specification:
Each input file contains one test case. Each case first gives a positive integer N (≤50) which is the total number of sets. Then N lines follow, each gives a set with a positive M (≤10
​4
​​ ) and followed by M integers in the range [0,10
​9
​​ ]. After the input of sets, a positive integer K (≤2000) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to N). All the numbers in a line are separated by a space.

Output Specification:
For each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.

Sample Input:
3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3
Sample Output:
50.0%
33.3%

Code:

#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
const int maxn = 60;
int total[maxn];
set<int> a[maxn];

int main(){
//    freopen("a.txt", "r", stdin);
    int n, m, a1, a2;
    int i, j, k;
    float tnum = 0, cnum = 0, p;
    scanf("%d", &n);
    for(i = 1; i <= n; ++i){
        scanf("%d", &m);
        for(j = 0; j < m; ++j){
            scanf("%d", &k);
            a[i].insert(k);
        }
        total[i] = a[i].size();
    }
    scanf("%d", &n);
    for(i = 0; i < n; ++i){
        cnum = 0;
        tnum = 0;
        scanf("%d %d",&a1,&a2);
        set<int>::iterator it1, it2 = a[a2].begin();
        for(it1 = a[a1].begin(); it1 != a[a1].end();++it1){
            if(a[a2].find(*(it1)) != a[a2].end()){
                cnum++;
            }
        }
        tnum = total[a1] + total[a2] - cnum;
        p = cnum / tnum * 100;
        printf("%.1f%\n",p);
    }
    return 0;
}
  1. PAT 1071 Speech Patterns (25 分)

Links:
https://pintia.cn/problem-sets/994805342720868352/problems/994805398257647616

People often have a preference among synonyms of the same word. For example, some may prefer “the police”, while others may prefer “the cops”. Analyzing such patterns can help to narrow down a speaker’s identity, which is useful when validating, for example, whether it’s still the same person behind an online avatar.

Now given a paragraph of text sampled from someone’s speech, can you find the person’s most commonly used word?

Input Specification:
Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return \n. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z].

Output Specification:
For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a “word” is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.

Note that words are case insensitive.

Sample Input:
Can1: “Can a can can a can? It can!”
Sample Output:
can 5

Code:

#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <map>
using namespace std;
map<string,int> mp;

bool isWord(char c){
    if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
        return true;
    else return false;
}

int main(){
//    freopen("a.txt", "r", stdin);
    string str, res;
    int maxl = -1;
    getline(cin, str);
    int len = str.length();
    for(int i = 0; i < len; ++i){
        string word;
        while(isWord(str[i])){
            if(str[i] >= 'A' && str[i] <= 'Z') str[i] += 32;
            word += str[i++];
        }
        if(word != ""){
            if(mp[word]) mp[word]++;
            else mp[word] = 1;
        }
    }
    map<string,int>::iterator it = mp.begin();
    for(it; it != mp.end(); it++){
        if(it->second > maxl){
            maxl = it->second;
            res = it->first;
        }
    }
    cout << res << " " << maxl << endl;
    
    return 0;
}

Guess you like

Origin blog.csdn.net/Void_worker/article/details/82819454