C - Phone Numbers

Vasya has several phone books, in which he recorded the telephone numbers of his friends. Each of his friends can have one or several phone numbers.

Vasya decided to organize information about the phone numbers of friends. You will be given n strings — all entries from Vasya’s phone books. Each entry starts with a friend’s name. Then follows the number of phone numbers in the current entry, and then the phone numbers themselves. It is possible that several identical phones are recorded in the same record.

Vasya also believes that if the phone number a is a suffix of the phone number b (that is, the number b ends up with a), and both numbers are written by Vasya as the phone numbers of the same person, then a is recorded without the city code and it should not be taken into account.

The task is to print organized information about the phone numbers of Vasya’s friends. It is possible that two different people have the same number. If one person has two numbers x and y, and x is a suffix of y (that is, y ends in x), then you shouldn’t print number x. If the number of a friend in the Vasya’s phone books is recorded several times in the same format, it is necessary to take it into account exactly once.

Read the examples to understand statement and format of the output better.

Input
First line contains the integer n (1 ≤ n ≤ 20) — number of entries in Vasya’s phone books.

The following n lines are followed by descriptions of the records in the format described in statement. Names of Vasya’s friends are non-empty strings whose length does not exceed 10. They consists only of lowercase English letters. Number of phone numbers in one entry is not less than 1 is not more than 10. The telephone numbers consist of digits only. If you represent a phone number as a string, then its length will be in range from 1 to 10. Phone numbers can contain leading zeros.

Output
Print out the ordered information about the phone numbers of Vasya’s friends. First output m — number of friends that are found in Vasya’s phone books.

The following m lines must contain entries in the following format “name number_of_phone_numbers phone_numbers”. Phone numbers should be separated by a space. Each record must contain all the phone numbers of current friend.

Entries can be displayed in arbitrary order, phone numbers for one record can also be printed in arbitrary order.

Examples
Input
2
ivan 1 00123
masha 1 00123
Output
2
masha 1 00123
ivan 1 00123
Input
3
karl 2 612 12
petr 1 12
katya 1 612
Output
3
katya 1 612
petr 1 12
karl 1 612
Input
4
ivan 3 123 123 456
ivan 2 456 456
ivan 8 789 3 23 6 56 9 89 2
dasha 2 23 789
Output
2
dasha 2 23 789
ivan 4 789 123 2 456

The simulation questions discuss in advance how to store, how to enumerate, how to transfer, and then pay attention to some details.
Modularization

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)

typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(int out = 0)
{
    
    
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 110;
const int M = 35;
const int mod = 1e9 + 7;
const int PP = 131;
const double eps = 1e-10;

int n;
set<string> Name, Num[N];

struct Node{
    
    
	string name;
	vector<string> num;
}node[N];

bool is_true(string a, string b){
    
    
	for (int i = 0; i < b.size(); i ++){
    
    
		if (a == b.substr(i))    return true;
	}
	return false;
}

signed main(){
    
    
//	if (is_true("a", "ba"))    cout << "---" << endl;
	
	gt(n);
	
	int idx = 1;
	for (int f = 1; f <= n; f ++){
    
    
		string str;
		cin >> str;
		bool flag = false;
		if (!Name.count(str)){
    
    
			Name.insert(str);
			node[idx].name = str;
			flag = true;
		}
		int cnt;
		cin >> cnt;
		while(cnt --){
    
    
			string s;
			cin >> s;
			if (flag){
    
    
				if (!Num[idx].count(s)){
    
    
					for (int k = 0; k < s.size(); k ++)    Num[idx].insert(s.substr(k));
					node[idx].num.push_back(s);
				}
			}
		   else{
    
    
		   		for (int k = 1; k < idx; k ++){
    
    
				     if (node[k].name == str){
    
    
				     	if (!Num[k].count(s)){
    
    
					   for (int l = 0; l < s.size(); l ++)    Num[k].insert(s.substr(l));
				  	    node[k].num.push_back(s);
				}
					 }
				}
		 	 }
		   }
		   if (flag)   idx ++;
	   }
	
	
	for (int i = 1; i < idx; i ++){
    
    
		for (int j = 0; j < node[i].num.size(); j ++){
    
    
			for (int k = 0; k < node[i].num.size(); k ++){
    
    
				if (k == j)   continue;
				if (is_true(node[i].num[j], node[i].num[k])){
    
    
					node[i].num.erase(node[i].num.begin() + j);
					j --;
					break;
				}
			}
		}
	}
	
	cout << idx - 1 << endl;
	for (int i = 1; i <= (idx - 1); i ++){
    
    
		cout << node[i].name << " ";
		cout << node[i].num.size() << " ";
		for (int j = 0; j < node[i].num.size(); j ++){
    
    
			cout << node[i].num[j] << " ";
		}
		cout << endl;
	}
	
	return 0;
}

F - Remove Extra One
You are given a permutation p of length n. Remove one element from permutation to make the number of records the maximum possible.

We remind that in a sequence of numbers a1, a2, …, ak the element ai is a record if for every integer j (1 ≤ j < i) the following holds: aj < ai.

Input
The first line contains the only integer n (1 ≤ n ≤ 105) — the length of the permutation.

The second line contains n integers p1, p2, …, pn (1 ≤ pi ≤ n) — the permutation. All the integers are distinct.

Output
Print the only integer — the element that should be removed to make the number of records the maximum possible. If there are multiple such elements, print the smallest one.

Examples
Input
1
1
Output
1
Input
5
5 1 2 3 4
Output
5
Note
In the first example the only element can be removed.

For this discussion of individuals, you can use the most classic contribution method to delete the contribution of each. The key is how to determine how much each contribution is. The first two maximum and minimum values ​​can be updated from the current state. Contributions, it may also update the later contributions, or may update the previous contributions

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#include <string>
#include <queue>
#include <map>
#include <stack>
#include <map>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <ext/rope>
#include <bits/stdc++.h> 

using namespace std;

#define gt(x) x = read()
#define int long long
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define x first
//#define y second

int dx[4] = {
    
    0, 1, 0, -1};
int dy[4] = {
    
    1, 0, -1, 0}; 

//typedef __int128 INT;
typedef pair<int, int> PII;
typedef unsigned long long ULL;

inline int read(int out = 0)
{
    
    
    char c;
    while((c=getchar()) < 48 || c > 57);
    while(c >= 48 && c <= 57) out=out*10+c-48,c=getchar();
    return out; 
}

const int N = 1e5 + 10;
const int M = 35;
const int mod = 1e9 + 7;
const int PP = 13331;
const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-10;
const double PI = 3.1415926;

int n;
int a[N];
int cnt[N];

signed main(){
    
    
	gt(n);
	for (int i = 1; i <= n; i ++){
    
    
		gt(a[i]);
	}
	
	int maxd1 = 0, maxd2 = 0;
	for (int i = 1; i <= n; i ++){
    
    
		if (a[i] > maxd1){
    
    
			cnt[a[i]] --;
			maxd2 = maxd1;
			maxd1 = a[i];
		}
		else if (a[i] > maxd2){
    
    
			cnt[maxd1] ++;
			maxd2 = a[i];
		}
	}
	
	int ans = 1;
	int res = 0;
	
	for (int i = 2; i <= n; i ++){
    
    
	//	cout << cnt[i] << endl;
		if (cnt[ans] < cnt[i]){
    
    
			ans = i;
			//res = a[i];
		}
	}
	
	cout << ans << endl;
	
	return 0;
}

-Pikachu had an array with him. He wrote down all the non-empty subsequences of the array on paper. Note that an array of size n has 2n - 1 non-empty subsequences in it.

Pikachu being mischievous as he always is, removed all the subsequences in which Maximum_element_of_the_subsequence  -  Minimum_element_of_subsequence  ≥ d

Pikachu was finally left with X subsequences.

However, he lost the initial array he had, and now is in serious trouble. He still remembers the numbers X and d. He now wants you to construct any such array which will satisfy the above conditions. All the numbers in the final array should be positive integers less than 1018.

Note the number of elements in the output array should not be more than 104. If no answer is possible, print  - 1.

Input
The only line of input consists of two space separated integers X and d (1 ≤ X, d ≤ 109).

Output
Output should consist of two lines.

First line should contain a single integer n (1 ≤ n ≤ 10 000)— the number of integers in the final array.

Second line should consist of n space separated integers — a1, a2, … , an (1 ≤ ai < 1018).

If there is no answer, print a single integer -1. If there are multiple answers, print any of them.

Examples
Input
10 5
Output
6
5 50 7 15 6 100
Input
4 2
Output
4
10 100 1000 10000
Note
In the output of the first example case, the remaining subsequences after removing those with Maximum_element_of_the_subsequence  -  Minimum_element_of_subsequence  ≥ 5 are [5], [5, 7], [5, 6], [5, 7, 6], [50], [7], [7, 6], [15], [6], [100]. There are 10 of them. Hence, the array [5, 50, 7, 15, 6, 100] is valid.

Similarly, in the output of the second example case, the remaining sub-sequences after removing those with Maximum_element_of_the_subsequence  -  Minimum_element_of_subsequence  ≥ 2 are [10], [100], [1000], [10000]. There are 4 of them. Hence, the array [10, 100, 1000, 10000] is valid.

10 5
Constructs a subsequence so that there are 10 of them whose range is no more than 5, which can be written as 11166

Any number such as 10 must be composed of the sum of a certain 1 in the binary system. Separate a sequence. The same number of 1 on each bit constitutes a 1, and then goes to the higher digits in turn. Each time the difference is constructed as 5

#include <stdio.h>
#include <math.h>
 
 
 
int main () {
    
    
	int a,b;
	scanf("%d %d",&a,&b);
	int i = 0;
	long long int tem = 1;
	int count = 0;
	long long int num[10000];
	while(a) {
    
    
		i = 0;
		while(pow(2,i+1) - 1 <= a) {
    
    
			i++;
		}
		for(int j = 0; j < i; j++) {
    
    
			num[count] = tem;
			count++;
		}
		a = a - (pow(2,i) - 1);
		tem += b;
	}
	printf("%d\n",count);
	for(int j = 0 ; j < count ; j++) {
    
    
		printf("%lld ",num[j]);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_45772483/article/details/112795586