STL some notes

1.set

Regarding set, what must be explained is set associative container. As a container, set is also used to store data types of the same data type, and can retrieve data from a data set. The value of each element in the set is unique, and the system can automatically sort according to the value of the element.

#include
set<type> Variable name
//s.end() has no value, points to the position after the last element
. Some usages of clear and erase in set.

Why does the previously saved iterator not become invalid after each insert?

 iterator这里就相当于指向节点的指针,内存没有变,指向内存的指针怎么会失效呢(当然被删除的那个元素本身已经失效了)。相对于vector来说,每一次删除和插入,指针都有可能失效,调用push_back在尾部插入也是如此。因为为了保证内部数据的连续存放,iterator指向的那块内存在删除和插入过程中可能已经被其他内存覆盖或者内存已经被释放了。即使时push_back的时候,容器内部空间可能不够,需要一块新的更大的内存,只有把以前的内存释放,申请新的更大的内存,复制已有的数据元素到新的内存,最后把需要插入的元素放到最后,那么以前的内存指针自然就不可用了。特别时在和find等算法在一起使用的时候,牢记这个原则:不要使用过期的iterator。

Insert picture description here

Solution:
Two people take turns to say a word of their own, but the word that cannot be said and the word that cannot be said, the person who cannot say the word loses the game.
If there is no intersection, n>m is the first person to win.
If there is an intersection: find out The same word k,
put all words in the set to remove the duplicate

Obviously, both people must first try to repeat the middle paragraph and take first;
each person can only take one word at a time;
then the odd number is beneficial to the first person;
then remove the repeated middle paragraph;
suppose that the second two are removed. The number of words of a person is x, y
, if the number of repetitions before is odd
x=x+1; if the number is
even, no processing is needed;
then compare x, y;
(The first person can speak at most words x = n-k+ (k+1)/2, the second person can say the most words y=m-k+k/2) The
same words are the first person loses,
because every time the first person finishes taking the second person A person has a lot of words;

Type: Gaming + Greedy? +stl

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<set>
#include<cstring>
#include<iostream>
using namespace std;
int main(){
    
    
	int n,m;
	string s;
	set<string> st;
	cin>>n>>m;
	int i,j;
	for(i=0;i<n+m;i++){
    
    
		cin>>s;
		
		st.insert(s);
	}
	int k=n+m-st.size();
	int x=n-k+(k+1)/2,y=m-k+k/2;
	
	if(x>y)
	printf("YES\n");
	else
	printf("NO\n");
	
}

2.multiset

Multiset is a kind of associative container, it is a sorted collection (the elements have been sorted), and the same elements are allowed.

You cannot directly modify the value of the element in the multiset container. Because after the element is modified, the container will not automatically re-adjust the order, so the order of the container will be destroyed, and then the search and other operations on it will get the wrong result. Therefore, if you want to modify the value of an element in a multiset container, the correct approach is to delete the element first, and then insert a new element.

Similar to all standard associative containers, sets and multisets are usually completed in a balanced binary tree.

The main advantage of automatic sorting is to make the binary tree search element with good performance, and its search function algorithm has logarithmic complexity. However, automatic sorting also poses a limitation. You cannot directly change the element value, because this will disrupt the original order. To change the value of an element, you must first delete the old element and then insert the new element. So sets and multisets have the following characteristics:

It does not provide any operation element directly used to access the element. Access to the element
through the iterator.

Insert picture description here
Problem solution:
This problem is actually a problem of finding intersection.

It can be implemented by comparing the array after sorting, or it can be implemented by STL. The former method is adopted here, just a simple calculation for sorting comparison.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<set>
#include<cstring>
#include<iostream>
#define N 10020
int a[N],b[N];
using namespace std;
int main(){
    
    
	int t,n,m;
	
	int i,j;
	cin>>t;
	

	while(t--){
    
    
		cin>>n>>m;
		for(i=0;i<n;i++){
    
    
			cin>>a[i];
			
		}
		for(i=0;i<m;i++){
    
    
			cin>>b[i];
		
		}
		sort(a,a+n);
		sort(b,b+m);
		i=0,j=0;
		int cnt=0;
		while(i<n&&j<m){
    
    
			if(a[i]==b[j]){
    
    
				i++,j++,cnt++;
			}else if(a[i]<b[j]){
    
    
				i++;
			}else{
    
    
				j++;
			}
		}
		printf("%d\n",n+m-2*cnt);
		
	}
	
}

or


#include <cstdio>
#include <set>
#include <cstdlib>
 
using namespace std;
 
int n, m;
multiset<int> s1, s2;
 
void input()
{
    
    
	s1.clear();
	s2.clear();
	
	int num;
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
    
    
		scanf("%d", &num);
		s1.insert(num);
	}
	
	for (int i = 0; i < m; i++) {
    
    
		scanf("%d", &num);
		s2.insert(num);
	}
}
 
void solve()
{
    
    
	int ans = 0;
	int prev;
	for (multiset<int>::iterator it = s2.begin(); it != s2.end(); it++) {
    
    
		if (it == s2.begin()) prev = *it;
		else if (*it == prev) continue;
		
		if (s1.count(*it)) {
    
    
			ans += abs((int)(s1.count(*it) - s2.count(*it)));
			s1.erase(*it);
		} else {
    
    
			ans += s2.count(*it);
		}
		
		prev = *it;
	}
	
	ans += s1.size();
	
	printf("%d\n", ans);
}
 
int main()
{
    
    
	#ifndef ONLINE_JUDGE
		freopen("d:\\OJ\\uva_in.txt", "r", stdin);
	#endif
	
	int t;
	scanf("%d", &t);
	for (int i = 1; i <= t; i++) {
    
    
		input();
		solve();
	}
	return 0;
}

//获得两个set的并
 36     set<int> eg3;
 37     cout << "Union(两个set的并集):";
 38     set_union(eg1.begin(),
 39         eg1.end(),
 40         eg2.begin(),
 41         eg2.end(),
 42         insert_iterator<set<int> >(eg3, eg3.begin())
 43         );//注意第五个参数的形式
 44     copy(eg3.begin(), eg3.end(), ostream_iterator<int>(cout, " "));
 45     cout << endl;
 46 
 47     //获得两个set的交,注意进行集合操作之前接收结果的set要调用clear()函数清空一下
 48     eg3.clear();
 49     set_intersection(eg1.begin(),
 50         eg1.end(),
 51         eg2.begin(),
 52         eg2.end(),
 53         insert_iterator<set<int> >(eg3, eg3.begin())
 54         );
 55     cout << "Intersection:";
 56     copy(eg3.begin(), eg3.end(), ostream_iterator<int>(cout, " "));
 57     cout << endl;
 58 
 59     //获得两个set的差
 60     eg3.clear();
 61     set_difference(eg1.begin(),
 62         eg1.end(), eg2.begin(),
 63         eg2.end(),
 64         insert_iterator<set<int> >(eg3, eg3.begin())
 65         );
 66     cout << "Difference:";
 67     copy(eg3.begin(), eg3.end(), ostream_iterator<int>(cout, " "));
 68     cout << endl;
 69 
 70     //获得两个set的对称差,也就是假设两个集合分别为A和B那么对称差为AUB-A∩B
 71     eg3.clear();
 72     set_symmetric_difference(eg1.begin(), eg1.end(), eg2.begin(), eg2.end(), insert_iterator<set<int> >(eg3, eg3.begin()));
 73     copy(eg3.begin(), eg3.end(), ostream_iterator<int>(cout, " "));
 74     cout << endl;
 75 

Guess you like

Origin blog.csdn.net/weixin_46064382/article/details/109558817