Acwing数据结构

Acwing数据结构

单链表

题目描述

实现一个单链表,链表初始为空,支持三种操作:

  1. 向链表头插入一个数;
  2. 删除第 k个插入的数后面的数;
  3. 在第 k 个插入的数后插入一个数。

现在要对该链表进行 M 次操作,进行完所有操作后,从头到尾输出整个链表。

注意:题目中第 k 个插入的数并不是指当前链表的第 k 个数。例如操作过程中一共插入了 n个数,则按照插入的时间顺序,这 n个数依次为:第 1个插入的数,第 2 个插入的数,…第 n 个插入的数。

输入格式

第一行包含整数 M,表示操作次数。

接下来 M行,每行包含一个操作命令,操作命令可能为以下几种:

  1. H x,表示向链表头插入一个数 x。
  2. D k,表示删除第 k 个插入的数后面的数(当 k 为 0 时,表示删除头结点)。
  3. I k x,表示在第 k 个插入的数后面插入一个数 x(此操作中 k均大于 0)。

输出格式

共一行,将整个链表从头到尾输出。

数据范围

1≤M≤100000

所有操作保证合法。

输入样例:

10
H 9
I 1 1
D 1
D 0
H 6
I 3 6
I 4 5
I 4 5
I 3 4
D 6

输出样例:

6 4 6 5

提交代码

#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;
int idx, head, e[N], ne[N];

void init()
{
    head = -1;
    idx = 0;
}

void add(int x)
{
    e[idx] = x,ne[idx] = head, head = idx++;
}

void add_k(int x, int k)
{
    e[idx] = x, ne[idx] = ne[k], ne[k] = idx++;
}

void remove(int k)
{
    ne[k] = ne[ne[k]];
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    init();
    int n;
    cin >> n;
    while(n--)
    {
        char op;
        cin >> op;
        int k, x;
        
        if(op == 'H')
        {
            cin >> x;
            add(x);
        }
        else if(op == 'D')
        {
            cin >> k;
            if(!k) head = ne[head];
            else remove(k - 1);
        }
        else
        {
            cin >> k >> x;
            add_k(x, k - 1);
        }
    }
    
    for(int i = head; i != -1; i = ne[i])
    {
        cout << e[i] <<" \n"[ne[i] == -1];
    }
    return 0;
}

双链表

题目描述

实现一个双链表,双链表初始为空,支持 5 种操作:

  1. 在最左侧插入一个数;
  2. 在最右侧插入一个数;
  3. 将第 k 个插入的数删除;
  4. 在第 k 个插入的数左侧插入一个数;
  5. 在第 k 个插入的数右侧插入一个数

现在要对该链表进行 M 次操作,进行完所有操作后,从左到右输出整个链表。

注意:题目中第 k 个插入的数并不是指当前链表的第 k 个数。例如操作过程中一共插入了 n 个数,则按照插入的时间顺序,这 n 个数依次为:第 1 个插入的数,第 2 个插入的数,…第 n 个插入的数。

输入格式

第一行包含整数 M,表示操作次数。

接下来 M 行,每行包含一个操作命令,操作命令可能为以下几种:

  1. L x,表示在链表的最左端插入数 x。
  2. R x,表示在链表的最右端插入数 x。
  3. D k,表示将第 k 个插入的数删除。
  4. IL k x,表示在第 k 个插入的数左侧插入一个数。
  5. IR k x,表示在第 k 个插入的数右侧插入一个数。

输出格式

共一行,将整个链表从左到右输出。

数据范围

1≤M≤100000

所有操作保证合法。

输入样例:

10
R 7
D 1
L 3
IL 2 10
D 3
IL 2 7
L 8
R 9
IL 4 7
IR 2 2

输出样例:

8 7 7 3 2 9
难度:简单
时/空限制:1s / 64MB
总通过数:44821
总尝试数:65023
来源:模板题,AcWing
算法标签

提交代码

// Problem: 双链表
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/829/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;

int m;
int e[N], l[N], r[N], idx;

void init()
{
	idx = 2;
	l[1] = 0;
	r[0] = 1;
}


void add(int a, int x)
{
	e[idx] = x;
	l[idx] = a, r[idx] = r[a];
	l[r[a]] = idx, r[a] = idx++;
}

void remove(int a)
{
	l[r[a]] = l[a];
	r[l[a]] = r[a];
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	cin >> m;
	init();
	while(m--)
	{
		string op;
		cin >> op;
		int k, x;
		if(op == "L")
		{
			cin >> x;
			add(0, x);
		}
		else if(op == "R")
		{
			cin >> x;
			add(l[1], x);
		}
		else if(op == "D")
		{
			cin >> k;
			remove(k + 1);
		}
		else if(op == "IL")
		{
			cin >> k >> x;
			add(l[k + 1], x);
		}
		else
		{
			cin >> k >> x;
			add(k + 1, x);
		}
		
		
	}
	
	for(int i = r[0]; i != 1; i = r[i])
	{
		cout << e[i] <<" \n"[r[i]==1];
	}
	return 0;
}

1.模拟栈

题目描述

实现一个栈,栈初始为空,支持四种操作:

  1. push x – 向栈顶插入一个数 x;
  2. pop – 从栈顶弹出一个数;
  3. empty – 判断栈是否为空;
  4. query – 查询栈顶元素。

现在要对栈进行 M 个操作,其中的每个操作 3 和操作 4 都要输出相应的结果。

输入格式

第一行包含整数 M,表示操作次数。

接下来 M 行,每行包含一个操作命令,操作命令为 push xpopemptyquery 中的一种。

输出格式

对于每个 emptyquery 操作都要输出一个查询结果,每个结果占一行。

其中,empty 操作的查询结果为 YESNOquery 操作的查询结果为一个整数,表示栈顶元素的值。

数据范围

1≤M≤100000,

1≤x≤\(10 ^ 9\)

所有操作保证合法。

输入样例:

10
push 5
query
push 6
pop
query
pop
empty
push 4
query
empty

输出样例:

5
5
YES
4
NO
难度:简单
时/空限制:1s / 64MB
总通过数:42255
总尝试数:60362
来源:模板题
算法标签

提交代码

// Problem: 模拟栈
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/830/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;


int main()
{
	int n, x;
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	string op;
	cin >> n;
	vector<int>  a(100005);
	int k = 0;
	while(n--)
	{
		cin >> op;
		if(op == "push")
		{
			cin >> x;
			a[k++] = x;
		}
		else if(op == "pop")
		{
			k--;
		}
		else if(op == "empty")
		{
			cout <<(k!=0?"NO":"YES") << "\n";
		}
		else
		{
			cout << a[k - 1] << "\n";
		}
	}
	
	
	return 0;
}

2.表达式求解

题目描述

给定一个表达式,其中运算符仅包含 +,-,*,/(加 减 乘 整除),可能包含括号,请你求出表达式的最终值。

注意:

  • 数据保证给定的表达式合法。
  • 题目保证符号 - 只作为减号出现,不会作为负号出现,例如,-1+2,(2+2)*(-(1+1)+2) 之类表达式均不会出现。
  • 题目保证表达式中所有数字均为正整数。
  • 题目保证表达式在中间计算过程以及结果中,均不超过 \(2^31\)
  • 题目中的整除是指向 0 取整,也就是说对于大于 0 的结果向下取整,例如 5/3=1,对于小于 0 的结果向上取整,例如 5/(1−4)=−1。
  • C++和Java中的整除默认是向零取整;Python中的整除//默认向下取整,因此Python的eval()函数中的整除也是向下取整,在本题中不能直接使用。

输入格式

共一行,为给定表达式。

输出格式

共一行,为表达式的结果。

数据范围

表达式的长度不超过 \(10^5\)

输入样例:

(2+2)*(1+1)

输出样例:

8
难度:简单
时/空限制:1s / 64MB
总通过数:28722
总尝试数:51085
来源:模板题,AcWing
算法标签

提交代码

// Problem: 表达式求值
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/3305/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;

stack<int> num;
stack<char> op;


void figure()
{
	auto a = num.top();num.pop();
	auto b = num.top();num.pop();
	auto c = op.top();op.pop();
	int x;
	if(c == '+') x = a + b;
	else if(c == '-') x = b - a;
	else if(c == '*') x = a * b;
	else x = b / a;
	num.push(x);
	// cout << b << " " << c << " " << a << " = " << x << "\n";
}


int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	unordered_map<char, int > priority{{'+',1},{'-',1},{'*',2},{'/',2}};
	string str;
	cin >> str;
	for(int i = 0; i < str.size(); i++)
	{
		auto c = str[i];
		if(isdigit(c))
		{
			int x = 0, j = i;
			while(j < str.size() && isdigit(str[j]))
			{
				x = x * 10 + str[j++] - '0';
			}
			
			i = j - 1;
			num.push(x);
		}
		else if(c == '(') op.push(c);
		else if(c == ')')
		{
			while(op.top() != '(') figure();
			op.pop();
		}
		else 
		{
			while(op.size() && op.top() != '(' && priority[op.top()] >= priority[c]) figure();
			op.push(c);
		}
	}
	
	while(op.size()) figure();
	cout << num.top() << "\n";
	return 0;
}

队列

模拟队列

题目描述

实现一个队列,队列初始为空,支持四种操作:

  1. push x – 向队尾插入一个数 x;
  2. pop – 从队头弹出一个数;
  3. empty – 判断队列是否为空;
  4. query – 查询队头元素。

现在要对队列进行 M 个操作,其中的每个操作 3 和操作 4 都要输出相应的结果。

输入格式

第一行包含整数 M,表示操作次数。

接下来 M 行,每行包含一个操作命令,操作命令为 push xpopemptyquery 中的一种。

输出格式

对于每个 emptyquery 操作都要输出一个查询结果,每个结果占一行。

其中,empty 操作的查询结果为 YESNOquery 操作的查询结果为一个整数,表示队头元素的值。

数据范围

1≤M≤100000,

1≤x≤\(10 ^ 9\),

所有操作保证合法。

输入样例:

10
push 6
empty
query
pop
empty
push 3
push 4
pop
query
push 6

输出样例:

NO
6
YES
4
难度:简单
时/空限制:1s / 64MB
总通过数:37654
总尝试数:51805
来源:模板题
算法标签

提交代码

// Problem: 模拟队列
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/831/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int hh, tt;
int q[N];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int m;
	cin >> m;
	tt = -1;
	while(m--)
	{
		string op;
		cin >> op;
		if(op == "push")
		{
			int x;
			cin >> x;
			q[++tt ]  = x;
		}
		else if(op == "pop")
		{
			hh++;
		}
		else if(op == "empty")
		{
			if(hh <= tt) cout << "NO\n";
			else cout << "YES\n";
		}
		else
		{
			cout << q[hh] << "\n";
		}
	}
	
	return 0;
}

单调栈

题目描述

给定一个长度为 N 的整数数列,输出每个数左边第一个比它小的数,如果不存在则输出 −1。

输入格式

第一行包含整数 N,表示数列长度。

第二行包含 N 个整数,表示整数数列。

输出格式

共一行,包含 N 个整数,其中第 i个数表示第 i 个数的左边第一个比它小的数,如果不存在则输出 −1。

数据范围

1≤N≤\(10^5\)

1≤数列中元素≤\(10^9\)

输入样例:

5
3 4 2 7 5

输出样例:

-1 3 -1 2 2
难度:简单
时/空限制:1s / 64MB
总通过数:68055
总尝试数:91989
来源:模板题
算法标签

提交代码

// Problem: 单调栈
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/832/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;

int n;
int stk[N];
int tt;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		int a;
		cin >> a;
		while(tt && stk[tt] >= a)
		{
			tt--;
		}
		if(tt) cout << stk[tt] << " ";
		else cout << "-1 ";
		stk[++tt] = a;
	}
	
	return 0;
}

单调队列

滑动窗口

题目描述

给定一个大小为 n≤\(10^6\)的数组。

有一个大小为 k 的滑动窗口,它从数组的最左边移动到最右边。

你只能在窗口中看到 k 个数字。

每次滑动窗口向右移动一个位置。

以下是一个例子:

该数组为 [1 3 -1 -3 5 3 6 7],k 为 3。

窗口位置 最小值 最大值
[1 3 -1] -3 5 3 6 7 -1 3
1 [3 -1 -3] 5 3 6 7 -3 3
1 3 [-1 -3 5] 3 6 7 -3 5
1 3 -1 [-3 5 3] 6 7 -3 5
1 3 -1 -3 [5 3 6] 7 3 6
1 3 -1 -3 5 [3 6 7] 3 7

你的任务是确定滑动窗口位于每个位置时,窗口中的最大值和最小值。

输入格式

输入包含两行。

第一行包含两个整数 n 和 k,分别代表数组长度和滑动窗口的长度。

第二行有 n 个整数,代表数组的具体数值。

同行数据之间用空格隔开。

输出格式

输出包含两个。

第一行输出,从左至右,每个位置滑动窗口中的最小值。

第二行输出,从左至右,每个位置滑动窗口中的最大值。

输入样例:

8 3
1 3 -1 -3 5 3 6 7

输出样例:

-1 -3 -3 -3 3 3
3 3 5 5 6 7
难度:简单
时/空限制:1s / 64MB
总通过数:72264
总尝试数:138052
来源:《算法竞赛进阶指南》, 模板题
算法标签

提交代码

// Problem: 滑动窗口
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/description/156/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, k;

int a[N], q[N];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n >> k;
	
	for(int i = 0; i < n; i++) cin >> a[i];
	
	int hh = 0, tt = -1;
	for(int i = 0; i < n; i++)
	{
		if(hh <= tt && i - k + 1 > q[hh]) hh++;
		
		while(hh <= tt && a[q[tt]] >= a[i]) tt--;
		q[++tt] = i;
		
		if(i >= k - 1) cout << a[q[hh]] << " ";
	}
	cout << "\n";
	hh = 0, tt = -1;
	for(int i = 0; i < n; i++)
	{
		if(hh <= tt && i - k + 1 > q[hh]) hh++;
		while(hh <= tt && a[q[tt]] <= a[i]) tt--;
		q[++tt] = i;
		if(i >= k - 1) cout << a[q[hh]] << " ";
	}
	
	return 0;
}

KMP

KMP字符串

题目描述

给定一个字符串 S,以及一个模式串 P,所有字符串中只包含大小写英文字母以及阿拉伯数字。

模式串 P 在字符串 S 中多次作为子串出现。

求出模式串 P 在字符串 S 中所有出现的位置的起始下标。

输入格式

第一行输入整数 N,表示字符串 P 的长度。

第二行输入字符串 P。

第三行输入整数 M,表示字符串 S 的长度。

第四行输入字符串 S。

输出格式

共一行,输出所有出现位置的起始下标(下标从 00 开始计数),整数之间用空格隔开。

数据范围

1≤N≤\(10^5\)

1≤M≤\(10^6\)

输入样例:

3
aba
5
ababa

输出样例:

0 2
难度:简单
时/空限制:1s / 256MB
总通过数:80593
总尝试数:155890
来源:模板题
算法标签

提交代码

// Problem: KMP字符串
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/833/
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10, M = 1e6 + 10;

int n, m;
int ne[N];
char s[M], p[M];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	cin >> n >> p + 1 >> m >> s + 1;
	
	for(int i = 2, j = 0; i <= n; i++) 	
	{
		while(j && p[i] != p[j + 1]) j = ne[j];
		if(p[i] == p[j + 1]) j++;
		ne[i] = j;
	}
	for(int i = 1, j = 0; i <= m; i++)
	{
		while(j && s[i] != p[j + 1]) j = ne[j];
		if(s[i] == p[j + 1]) j++;
		if(j == n)
		{
			cout << i - n << " ";
			j = ne[j];
		}
	}
	
	return 0;
	
}

Tire

Trie字符串统计

题目描述

维护一个字符串集合,支持两种操作:

  1. I x 向集合中插入一个字符串 x;
  2. Q x 询问一个字符串在集合中出现了多少次。

共有 N 个操作,所有输入的字符串总长度不超过 \(10^5\),字符串仅包含小写英文字母。

输入格式

第一行包含整数 N,表示操作数。

接下来 N 行,每行包含一个操作指令,指令为 I xQ x 中的一种。

输出格式

对于每个询问指令 Q x,都要输出一个整数作为结果,表示 x 在集合中出现的次数。

每个结果占一行。

数据范围

1≤N≤2∗\(10^4\)

输入样例:

5
I abc
Q abc
Q ab
I ab
Q ab

输出样例:

1
0
1
难度:简单
时/空限制:1s / 64MB
总通过数:67270
总尝试数:92806
来源:模板题
算法标签

提交代码

// Problem: Trie字符串统计
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/837/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)


#include<bits/stdc++.h>
using namespace std;


const int N = 1e5 + 10;

int son[N][26], cnt[N], idx;


void insert(string str)
{
	int p = 0;
	for(int i = 0; i < str.size() ;i++)
	{
		int u = str[i] - 'a';
		if(!son[p][u]) son[p][u] = ++idx;
		p = son[p][u];
	}
	cnt[p]++;
}


int query(string str)
{
	int p = 0;
	for(int i = 0; i < str.size(); i++)
	{
		int u = str[i] - 'a';
		if(!son[p][u]) return 0;
		p = son[p][u];
	}
	return cnt[p];
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n;
	cin >> n;
	while(n--)
	{
		string op, str;
		cin >> op >> str;
		if(op == "I") insert(str);
		else cout << query(str) << "\n";
	}
	return 0;
}

最大异或对

题目描述

在给定的 N 个整数 A1,A2……AN 中选出两个进行 xor(异或)运算,得到的结果最大是多少?

输入格式

第一行输入一个整数 N。

第二行输入 N 个整数 \(A_1\)\(A_N\)

输出格式

输出一个整数表示答案。

数据范围

1≤N≤\(10^5\),

0≤\(A_i\)<\(2^31\)

输入样例:

3
1 2 3

输出样例:

3
难度:简单
时/空限制:1s / 64MB
总通过数:48177
总尝试数:95578
来源:《算法竞赛进阶指南》, 模板题
算法标签

提交代码

// Problem: 最大异或对
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/145/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10, M = 3100005;
int a[N], son[M][2], idx;


void insert(int x)
{
	int p = 0;
	for(int i = 30; i >= 0; i--)
	{
		int u = x >> i & 1;
		if(!son[p][u]) son[p][u] = ++idx;
		p = son[p][u];
	}

}

int search(int x)
{
	int ans = 0, p = 0;
	
	for(int i = 30; i >= 0; i--)
	{
		int u = x >> i & 1;
		if(son[p][!u])
		{
			ans += 1 << i;
			p = son[p][!u];
		}
		else p = son[p][u];
	}
	return ans;
}


int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
		insert(a[i]);
	}
	
	int res = 0;
	for(int i = 0; i < n; i++) res = max(res, search(a[i]));
	
	
	cout << res << "\n";
	
	return 0;
}

并查集

1.合并集合

题目描述

一共有 n 个数,编号是 1∼n,最开始每个数各自在一个集合中。

现在要进行 m 个操作,操作共有两种:

  1. M a b,将编号为 a 和 b 的两个数所在的集合合并,如果两个数已经在同一个集合中,则忽略这个操作;
  2. Q a b,询问编号为 a 和 b 的两个数是否在同一个集合中;

输入格式

第一行输入整数 n 和 m。

接下来 m 行,每行包含一个操作指令,指令为 M a bQ a b 中的一种。

输出格式

对于每个询问指令 Q a b,都要输出一个结果,如果 a 和 b 在同一集合内,则输出 Yes,否则输出 No

每个结果占一行。

数据范围

1≤n,m≤\(10^5\)

输入样例:

4 5
M 1 2
M 3 4
Q 1 2
Q 1 3
Q 3 4

输出样例:

Yes
No
Yes
难度:简单
时/空限制:1s / 64MB
总通过数:65861
总尝试数:104226
来源:模板题,AcWing
算法标签

提交代码

// Problem: 合并集合
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/838/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;


struct DSU
{
	vector<int> f,siz;
	DSU(int n):f(n),siz(n, 1){iota(f.begin(), f.end(), 0);}
	int leader(int x)
	{
		while(x != f[x]) x = f[x] = f[f[x]];
		return x;
	}
	
	bool same(int u, int v)
	{
		return leader(u) == leader(v);
	}
	
	bool merge(int u, int v)
	{
		u = leader(u);
		v = leader(v);
		if(u == v) return false;
		f[v] = u;
		siz[u] += siz[v];
		return true;
	}
	
	int size(int x)
	{
		return siz[leader(x)];
	}
};


int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n, m;
	cin >> n >> m;
	DSU dsu(n + 1);
	for(int i = 0; i < m; i++)
	{
		string op;
		cin >> op;
		int u, v;
		if(op == "M")
		{
			cin >> u >> v;
			dsu.merge(u, v);
		}
		else
		{
			cin >> u >> v;
			if(dsu.same(u,v)) puts("Yes");
			else puts("No");
		}
		
	}
	return 0;
}

2.连通块中点的数量

题目描述

给定一个包含 n 个点(编号为 1∼n)的无向图,初始时图中没有边。

现在要进行 m 个操作,操作共有三种:

  1. C a b,在点 a 和点 b 之间连一条边,a 和 b 可能相等;
  2. Q1 a b,询问点 a 和点 b 是否在同一个连通块中,a 和 b 可能相等;
  3. Q2 a,询问点 a 所在连通块中点的数量;

输入格式

第一行输入整数 n 和 m。

接下来 m 行,每行包含一个操作指令,指令为 C a bQ1 a bQ2 a 中的一种。

输出格式

对于每个询问指令 Q1 a b,如果 a 和 b 在同一个连通块中,则输出 Yes,否则输出 No

对于每个询问指令 Q2 a,输出一个整数表示点 a 所在连通块中点的数量

每个结果占一行。

数据范围

1≤n,m≤\(10^5\)

输入样例:

5 5
C 1 2
Q1 1 2
Q2 1
C 2 5
Q2 5

输出样例:

Yes
2
3
难度:简单
时/空限制:1s / 64MB
总通过数:46310
总尝试数:91481
来源:模板题
算法标签

提交代码

// Problem: 连通块中点的数量
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/839/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;



struct DSU
{
	vector<int> f,siz;
	DSU(int n):f(n),siz(n,1){iota(f.begin(), f.end(), 0);}
	int leader(int x)
	{
		while(x != f[x]) x = f[x] = f[f[x]];
		return x;
	}
	
	bool same(int u, int v)
	{
		return leader(u) == leader(v);
	}
	
	bool merge(int u, int v)
	{
		u = leader(u);
		v = leader(v);
		if(u == v) return false;
		f[u] = v;
		siz[v] += siz[u];
		return true;
	}
	
	int size(int x)
	{
		return siz[leader(x)];
	}
};


int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, m;
	cin >> n >> m;
	DSU dsu(n + 1);
	for(int i = 0; i < m; i++)
	{
		string op;
		cin >> op;
		if(op == "C")
		{
			int u, v;
			cin >> u >> v;
			dsu.merge(u, v);
		}
		else if( op == "Q1")
		{
			int u, v;
			cin >> u >> v;
			if(dsu.same(u, v)) cout << "Yes\n"; 
			else cout << "No\n";
		}
		else
		{
			int a;
			cin >> a;
			cout << dsu.size(a) << "\n"; 
		}
	}
	return 0;
	
}

3.食物链

题目描述

动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形。

A 吃 B,B 吃 C,C 吃 A。

现有 N 个动物,以 1∼N 编号。

每个动物都是 A,B,C,中的一种,但是我们并不知道它到底是哪一种。

有人用两种说法对这 N 个动物所构成的食物链关系进行描述:

第一种说法是 1 X Y,表示 X 和 Y 是同类。

第二种说法是 2 X Y,表示 X 吃 Y。

此人对 N 个动物,用上述两种说法,一句接一句地说出 K 句话,这 K 句话有的是真的,有的是假的。

当一句话满足下列三条之一时,这句话就是假话,否则就是真话。

  1. 当前的话与前面的某些真的话冲突,就是假话;
  2. 当前的话中 X 或 Y 比 N 大,就是假话;
  3. 当前的话表示 X 吃 X,就是假话。

你的任务是根据给定的 N 和 K 句话,输出假话的总数。

输入格式

第一行是两个整数 N 和 K,以一个空格分隔。

以下 K 行每行是三个正整数 D,X,Y,两数之间用一个空格隔开,其中 D 表示说法的种类。

若 D=1,则表示 X 和 Y 是同类。

若 D=2,则表示 X 吃 Y。

输出格式

只有一个整数,表示假话的数目。

数据范围

1≤N≤50000,

0≤K≤100000

输入样例:

100 7
1 101 1 
2 1 2
2 2 3 
2 3 3 
1 1 3 
2 3 1 
1 5 5

输出样例:

3
难度:中等
时/空限制:1s / 64MB
总通过数:39351
总尝试数:84952
来源:《算法竞赛进阶指南》, 模板题 , NOI2001 , POJ1182 , kuangbin专题
算法标签

提交代码

// Problem: 食物链
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/description/242/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;

int n, m;


struct DSU
{
	vector<int> f,siz, dist;
	DSU(int n):f(n),siz(n,1), dist(n){iota(f.begin(), f.end(), 0);}
	int leader(int x)
	{
		if(f[x] != x)
		{
			int t = leader(f[x]);
			dist[x] += dist[f[x]];
			f[x] = t;
		}
		return f[x];
	}
	
	bool same(int u, int v)
	{
		return leader(u) == leader(v);
	}
	
	bool merge(int u, int v, int op)
	{
		int x = u, y = v;
		u = leader(u);
		v = leader(v);
		if(u == v) return false;
		f[u] = v;
		siz[v] += siz[u];
		if(op == 1) dist[u] = dist[y] - dist[x];
		else dist[u] = dist[y] + 1 - dist[x];
		return true;
	}
	
	int size(int x)
	{
		return siz[leader(x)];
	}
	int d(int x)
	{
		return dist[x];
	}
};

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	cin >> n >>  m;
	DSU dsu(n + 1);
	
	int res = 0;
	for(int i = 0; i < m; i++)
	{
		int op, x, y;
		cin >> op >> x >> y;
		if(x > n || y > n) res++;
		else
		{
			if(op == 1)
			{
				if(dsu.same(x,y) &&  (dsu.d(y) - dsu.d(x)) % 3) res++;
				else if(!dsu.same(x, y))
				{
					dsu.merge(x, y, 1);
				}
			}
			else
			{
				if(dsu.same(x, y) && (dsu.d(y) - dsu.d(x) + 1) % 3) res++;
				else if(!dsu.same(x, y))
				{
					dsu.merge(x, y, 2);
				}
			}
			
		}

	}
	cout << res << "\n";
	
	
	return 0;
}

1.堆排序

题目描述

输入一个长度为 n 的整数数列,从小到大输出前 m 小的数。

输入格式

第一行包含整数 n 和 m。

第二行包含 n 个整数,表示整数数列。

输出格式

共一行,包含 m 个整数,表示整数数列中前 m 小的数。

数据范围

1≤m≤n≤\(10^5\)

1≤数列中元素≤\(10^9\)

输入样例:

5 3
4 5 1 3 2

输出样例:

1 2 3
难度:简单
时/空限制:1s / 64MB
总通过数:55120
总尝试数:84160
来源:模板题,AcWing
算法标签

提交代码

// Problem: 堆排序
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/840/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m;
int h[N], cnt;
void down(int u)
{
	int t = u;
	if(u * 2 <= cnt && h[u * 2] < h[t] ) t = u * 2;
	if(u * 2 + 1 <= cnt && h[u * 2 + 1] < h[t]) t = u * 2 + 1;
	
	if(u != t)
	{
		swap(h[u], h[t]);
		down(t);
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	cin >> n >> m;
	for(int i = 1; i <= n; i++) cin >> h[i];
	cnt = n;
	for(int i = n / 2; i ; i--) down(i);
	
	while(m--)
	{
		cout << h[1] << " ";
		h[1] = h[cnt--];
		down(1);
	}
	
	return 0;
}

2.模拟堆

题目描述

维护一个集合,初始时集合为空,支持如下几种操作:

  1. I x,插入一个数 x;
  2. PM,输出当前集合中的最小值;
  3. DM,删除当前集合中的最小值(数据保证此时的最小值唯一);
  4. D k,删除第 k 个插入的数;
  5. C k x,修改第 k 个插入的数,将其变为 x;

现在要进行 N 次操作,对于所有第 2个操作,输出当前集合的最小值。

输入格式

第一行包含整数 N。

接下来 N 行,每行包含一个操作指令,操作指令为 I xPMDMD kC k x 中的一种。

输出格式

对于每个输出指令 PM,输出一个结果,表示当前集合中的最小值。

每个结果占一行。

数据范围

1≤N≤\(10^5\)

\(10^9\)≤x≤\(10^9\)

数据保证合法。

输入样例:

8
I -10
PM
I -10
D 1
C 2 8
I 6
PM
DM

输出样例:

-10
6
难度:简单
时/空限制:1s / 64MB
总通过数:34546
总尝试数:86496
来源:模板题
算法标签

提交代码

// Problem: 模拟堆
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/841/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;

int h[N],hp[N], ph[N],cnt;

void heap_swap(int u, int v)
{
	swap(ph[hp[u]], ph[hp[v]]);
	swap(hp[u], hp[v]);
	swap(h[u], h[v]);
}

void down(int u)
{
	int t = u;
	if(u * 2 <= cnt && h[u * 2] < h[t]) t = u * 2;
	if(u * 2 + 1 <= cnt && h[u * 2 + 1] < h[t]) t = u * 2 + 1;
	if(t != u)
	{
		heap_swap(u, t);
		down(t);
	}
}

void up(int u)
{
	while(u >> 1 && h[u] < h[u >> 1])
	{
		heap_swap(u, u >> 1);
		u >>= 1;
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n ,m = 0;
	cin >> n;
	while(n--)
	{
		string op;
		cin >> op;
		if(op == "I")
		{
			int x;
			cin >> x;
			m++;
			cnt++;
			ph[m] = cnt, hp[cnt] = m;
			h[cnt] = x;
			up(cnt);
		}
		else if(op == "PM") cout << h[1] << "\n";
		else if(op == "DM")
		{
			heap_swap(1,cnt);
			cnt--;
			down(1);
		} 
		else if(op == "D")
		{
			int k;
			cin >> k;
			k = ph[k];
			heap_swap(k, cnt);
			cnt--;
			up(k);
			down(k);
		}
		else
		{
			int x, k;
			cin >> k >> x;
			k = ph[k];
			h[k] = x;
			up(k);
			down(k);
		}
	}
	return 0;
}

哈希表

1.模拟散列表

题目描述

维护一个集合,支持如下几种操作:

  1. I x,插入一个数 x;
  2. Q x,询问数 x 是否在集合中出现过;

现在要进行 N 次操作,对于每个询问操作输出对应的结果。

输入格式

第一行包含整数 N,表示操作数量。

接下来 N 行,每行包含一个操作指令,操作指令为 I xQ x 中的一种。

输出格式

对于每个询问指令 Q x,输出一个询问结果,如果 x 在集合中出现过,则输出 Yes,否则输出 No

每个结果占一行。

数据范围

1≤N≤\(10^5\)

\(10^9\)≤x≤\(10^9\)

输入样例:

5
I 1
I 2
I 3
Q 2
Q 5

输出样例:

Yes
No
难度:简单
时/空限制:1s / 64MB
总通过数:67678
总尝试数:101791
来源:模板题
算法标签

提交代码

  • 拉链法
// Problem: 模拟散列表
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/description/842/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int e[N], ne[N], idx;
vector<int> h(N,-1);

void insert(int x)
{
	int k = (x % N + N) % N;
	e[idx] = x;
	ne[idx] = h[k];
	h[k] = idx++;
}

bool query(int x)
{
	int k = (x % N + N) % N;
	for(int i = h[k]; i != -1; i = ne[i])
	{
		int v = e[i];
		if(v == x) return true;
	}
	
	return false;
}


int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	
	int n;
	cin >> n;
	while(n--)
	{
		string op;
		int x;
		cin >> op >> x;
		if(op == "I") insert(x);
		else
		{
			if(query(x)) puts("Yes");
			else puts("No");
		}
	}

	return 0;
}
  • 开放寻址法
// Problem: 模拟散列表
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/842/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, INF = 0x3f3f3f3f;

vector<int> h(N,INF);

int find(int x)
{
	int t = (x % N + N) % N;
	while(h[t] != INF && h[t] != x)
	{
		t++;
		if(t == N) t = 0;
	}
	return t;
}


int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n;
	cin >> n;
	while(n--)
	{
		string op;
		int x;
		cin >> op >> x;
		if(op == "I")
		{
			h[find(x)] = x;
		}
		else 
		{
			if(h[find(x)] == INF) puts("No");
			else puts("Yes");
		}
	}
	return 0;
}

2.字符串哈希

题目描述

给定一个长度为 n 的字符串,再给定 m 个询问,每个询问包含四个整数 \(l_1,r_1,l_2,r_2\),请你判断 [\(l_1,r_1\)]和 [\(l_2,r_2\)] 这两个区间所包含的字符串子串是否完全相同。

字符串中只包含大小写英文字母和数字。

输入格式

第一行包含整数 n 和 m,表示字符串长度和询问次数。

第二行包含一个长度为 n 的字符串,字符串中只包含大小写英文字母和数字。

接下来 m 行,每行包含四个整数\(l_1,r_1,l_2,r_2\),表示一次询问所涉及的两个区间。

注意,字符串的位置从 1 开始编号。

输出格式

对于每个询问输出一个结果,如果两个字符串子串完全相同则输出 Yes,否则输出 No

每个结果占一行。

数据范围

1≤n,m≤\(10^5\)

输入样例:

8 3
aabbaabb
1 3 5 7
1 3 6 8
1 2 1 2

输出样例:

Yes
No
Yes
难度:简单
时/空限制:1s / 64MB
总通过数:41916
总尝试数:63089
来源:模板题
算法标签

提交代码

// Problem: 字符串哈希
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/843/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;

typedef unsigned long long ULL;

const int N = 1e5 + 10, P = 131;

int n, m;
string str;
ULL h[N], p[N];

ULL get(int l, int r)
{
	return h[r] - h[l - 1] * p[r - l + 1]; 
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n, m;
	cin >> n >> m;
	cin >> str;
	p[0] = 1;
	for(int i = 1; i <= n; i++)
	{
		h[i] = h[i - 1] * P + str[i - 1];
		p[i] = p[i - 1] * P;
	}
	
	while(m--)
	{
		int l1, r1, l2, r2;
		cin >> l1 >> r1 >> l2 >>r2;
		
		if(get(l1,r1) == get(l2, r2)) puts("Yes");
		else puts("No");
	}
	return 0;
}
posted @ 2023-03-03 16:25  哲远甄骏  阅读(30)  评论(0)    收藏  举报