链表
单链表
#include<iostream>
using namespace std;
const int N=100010;
int idx,head,n[N],ne[N];
int a;
void add_head(int x){
n[idx]=x;
ne[idx]=head;
head=idx++;
}
void add(int k,int x){
n[idx]=x;
ne[idx]=ne[k];
ne[k]=idx++;
}
void remove(int k){
ne[k]=ne[ne[k]];
}
int main(){
head=-1;idx=0;
cin>>a;
while(a--){
string op;
int k,x;
cin>>op;
if(op=="D")
{
cin>>k;
if(!k)head=ne[head];
remove(k-1);
}
else if(op=="H")
{
cin>>x;
add_head(x);
}
else if(op=="I"){
int k,x;
cin>>k>>x;
add(k-1,x);
}
}
for(int i=head;i!=-1;i=ne[i])
cout<<n[i]<<" ";
return 0;
}
双链表
#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int m;
int e[N],l[N],r[N],idx;
void init(){
r[0]=1;
l[1]=0;
idx=2;
}
void add(int k,int x){
e[idx]=x;
r[idx]=r[k];
l[idx]=k;
l[r[k]]=idx;
r[k]=idx;
}
void remove(int k){
r[l[k]]=r[k];
l[r[k]]=l[k];
}
单调栈
#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int n;
int tt,stk[N];
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cin>>n;
for(int i=0;i<n;i++){
int x;cin>>x;
while(tt&&stk[tt]>=x) tt--;
if(tt) cout<<stk[tt]<<' ';
else cout<<-1<<" ";
stk[++tt]=x;
}
return 0;
}
单调队列
#include<bits/stdc++.h>
using namespace std;
const int N=1000100;
int n,k;
int a[N],q[N];
int main(){
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++) scanf("%d",&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) printf("%d ",a[q[hh]]);
}
cout<<endl;
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) printf("%d ",a[q[hh]]);
}
return 0;
}
KMP算法
for (int i = 2, j = 0; i <= m; 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 <= n; i ++ )
{
while (j && s[i] != p[j + 1]) j = ne[j];
if (s[i] == p[j + 1]) j ++ ;
if (j == m)
{
j = ne[j];
}
}
Tire树
在这里插入代码片#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int son[N][26],cnt[N],idx;
char str[N];
void insert(char str[]){
int p=0;
for(int i=0;str[i];i++){
int u=str[i]-'a';
if(son[p][u]==0) son[p][u]=++idx;
p=son[p][u];
}
cnt[p]++;
}
int query(char str[]){
int p=0;
for(int i=0;str[i];i++){
int u=str[i]-'a';
if(son[p][u]==0) return 0;
p=son[p][u];
}
return cnt[p];
}
int main(){
int n;cin>>n;
while(n--){
char op[2];
scanf("%s%s",op,str);
if(op[0]=='I') insert(str);
else cout<<query(str)<<endl;
}
return 0;
}
并查集
#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int p[N];
int find(int x){
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
int main()
{
int n,m;cin>>n>>m;
for(int i=1;i<=n;i++) p[i]=i;
while(m--){
int a,b;
char op[2];
cin>>op[0]>>a>>b;
if(op[0]=='M') p[find(a)]=find(b);
else{
if(find(a)==find(b)) puts("Yes");
else puts("No");
}
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=100100;
int p[N];
int size1[N];
int find(int x){
if(p[x]!=x) p[x]=find(p[x]);
return p[x];
}
int main()
{
int n,m;cin>>n>>m;
for(int i=1;i<=n;i++)
{
p[i]=i;
size1[i]=1;
}
while(m--){
char op[5];
int a,b;
scanf("%s",op);
if(op[0]=='C')
{
cin>>a>>b;
if(find(a)==find(b)) continue;
size1[find(b)]+=size1[find(a)];
p[find(a)]=find(b);
}
else if(op[1]=='1')
{
cin>>a>>b;
if(find(a)==find(b)) puts("Yes");
else puts("No");
}
else {
cin>>a;
cout<<size1[find(a)]<<endl;
}
}
return 0;
}
哈希表
开放定址法
#include<bits/stdc++.h>
using namespace std;
const int N=200003,null=0x3f3f3f3f;
int h[N];
int find(int x)
{
int k=(x%N+N)%N;
while(h[k]!=null&&h[k]!=x)
{
k++;
if(k==N) k=0;
}
return k;
}
int main(){
int n;cin>>n;
memset(h,0x3f,sizeof h);
while(n--)
{
char op[2];
int x;
scanf("%s%d",op,&x);
int k=find(x);
if(*op=='I') h[k]=x;
else
{
if(h[k]!=null) puts("Yes");
else puts("No");
}
}
return 0;
}
拉链法
#include<bits/stdc++.h>
using namespace std;
const int N=100003;
int h[N],e[N],ne[N],idx;
void insert(int x){
int k=(x%N+N)%N;
e[idx]=x;
ne[idx]=h[k];
h[k]=idx++;
}
bool find(int x)
{
int k=(x%N+N)%N;
for(int i=h[k];i!=-1;i=ne[i]){
if(e[i]==k) return true;
}
return false;
}
int main(){
int n;cin>>n;
memset(h,-1,sizeof h);
while(n--){
char op[2];
int x;
scanf("%s%d",op,&x);
if(*op=='I')insert(x);
else{
if(find(x)) puts("Yes");
else puts("No");
}
}
return 0;
}