1,单调队列;
(76条消息) 单调队列专题_Dull丶的博客-CSDN博客
2,kmp算法;
先是自己和自己匹配,求出ne数组,然后和另一串匹配,进行求解;
循环里三步:while,if,记录ne数组/挪串匹配;
- #pragma GCC optimize(3)
- #include
- #define rep1(i,a,n) for( int i=(a);i<(n);++i)
- #define rep2(i,a,n) for( int i=(a);i<=(n);++i)
- #define per1(i,n,a) for( int i=(n);i>(a);i--)
- #define per2(i,n,a) for( int i=(n);i>=(a);i--)
- #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
- #define memset(a,i,b) memset((a),(i),sizeof (b))
- #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
- #define endl "\n"
- #define lowbit(m) ((-m)&(m))
- #define dbug(y) cout<<(y)<<"\n"
- #define dbug2(a,b) cout<<(a)<<" "<<(b)<<"\n"
- #define dbug3(a,b,c) cout<<(a)<<" "<<(b)<<" "<<(c)<<"\n"
- #define dbug4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<"\n"
- #define tulun int e[N],ne[N],h[N],w[N],idx
- #define T_solve() int T;cin>>T;while(T--)solve()
- #define pi 3.14159265358979323846
- using namespace std;
- typedef long long LL;
- typedef unsigned long long ULL;
- typedef pair<int,int> PII;
- typedef pair<long long,long long> PLL;
- typedef double dob;
- const int N=1e6+10,mod=1e6+3,base=131;
- char s[N],p[N];
- int ne[N];
- int n,m;
- signed main()
- {
- quick_cin();
- 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<
" "; - j=ne[j];
- }
- }
- return 0;
- }
3,trie树,最大异或和;
- #pragma GCC optimize(3)
- #include
- #define rep1(i,a,n) for( int i=(a);i<(n);++i)
- #define rep2(i,a,n) for( int i=(a);i<=(n);++i)
- #define per1(i,n,a) for( int i=(n);i>(a);i--)
- #define per2(i,n,a) for( int i=(n);i>=(a);i--)
- #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
- #define memset(a,i,b) memset((a),(i),sizeof (b))
- #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
- #define endl "\n"
- #define lowbit(m) ((-m)&(m))
- #define dbug(y) cout<<(y)<<"\n"
- #define dbug2(a,b) cout<<(a)<<" "<<(b)<<"\n"
- #define dbug3(a,b,c) cout<<(a)<<" "<<(b)<<" "<<(c)<<"\n"
- #define dbug4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<"\n"
- #define tulun int e[N],ne[N],h[N],w[N],idx
- #define T_solve() int T;cin>>T;while(T--)solve()
- #define pi 3.14159265358979323846
- using namespace std;
- typedef long long LL;
- typedef unsigned long long ULL;
- typedef pair<int,int> PII;
- typedef pair<long long,long long> PLL;
- typedef double dob;
- const int N=1e6+10,mod=1e6+3,base=131;
- int a[N],son[N<<2][2],idx;
- int n;
- void insert(int x)
- {
- int p=0;
- per2(i,30,0)
- {
- int s=x>>i&1;
- if(!son[p][s])son[p][s]=++idx;
- p=son[p][s];
- }
- }
- int search(int x)
- {
- int p=0,res=0;
- per2(i,30,0)
- {
- int s=x>>i&1;
- if(son[p][!s])
- {
- res+=1<
- p=son[p][!s];
- }
- else p=son[p][s];
- }
- return res;
- }
- signed main()
- {
- quick_cin();
- cin>>n;
- rep2(i,1,n)
- {
- cin>>a[i];
- insert(a[i]);
- }
- int ans=0;
- rep2(i,1,n)
- {
- ans=max(ans,search(a[i]));
- }
- dbug(ans);
- return 0;
- }
4,n-皇后问题
对角线及反对角线的状态表示;
正对角线就是y+x,该线上y+x都一样
反对角线y-x都一样,但是数组下标不能为0,故统一加n;
- #pragma GCC optimize(3)
- #include
- #define rep1(i,a,n) for( int i=(a);i<(n);++i)
- #define rep2(i,a,n) for( int i=(a);i<=(n);++i)
- #define per1(i,n,a) for( int i=(n);i>(a);i--)
- #define per2(i,n,a) for( int i=(n);i>=(a);i--)
- #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
- #define memset(a,i,b) memset((a),(i),sizeof (b))
- #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
- #define endl "\n"
- #define lowbit(m) ((-m)&(m))
- #define dbug(y) cout<<(y)<<"\n"
- #define dbug2(a,b) cout<<(a)<<" "<<(b)<<"\n"
- #define dbug3(a,b,c) cout<<(a)<<" "<<(b)<<" "<<(c)<<"\n"
- #define dbug4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<"\n"
- #define tulun int e[N],ne[N],h[N],w[N],idx
- #define T_solve() int T;cin>>T;while(T--)solve()
- #define pi 3.14159265358979323846
- using namespace std;
- typedef long long LL;
- typedef unsigned long long ULL;
- typedef pair<int,int> PII;
- typedef pair<long long,long long> PLL;
- typedef double dob;
- const int N=1e2+10,mod=1e6+3,base=131;
- int n;
- char g[N][N];
- int col[N],dg[N],udg[N];
- void dfs(int u)
- {
- if(u==n)
- {
- rep1(i,0,n)dbug(g[i]);
- cout<
- return;
- }
- int x=u;
- rep1(y,0,n)
- {
- if(col[y]||dg[y+x]||udg[y-x+n])continue;
- col[y]=dg[y+x]=udg[y-x+n]=1;
- g[x][y]='Q';
- dfs(x+1);
- g[x][y]='.';
-
- col[y]=dg[y+x]=udg[y-x+n]=0;
- }
- }
- signed main()
- {
- quick_cin();
- cin>>n;
- rep1(i,0,n)
- rep1(j,0,n)g[i][j]='.';
- dfs(0);
- return 0;
- }
5,走迷宫;
走到终点的最短路径,第一次走到的一定是最短的!因为是层向拓展,所以第一次走到的一定是最短的!;
- #pragma GCC optimize(3)
- #include
- #define rep1(i,a,n) for( int i=(a);i<(n);++i)
- #define rep2(i,a,n) for( int i=(a);i<=(n);++i)
- #define per1(i,n,a) for( int i=(n);i>(a);i--)
- #define per2(i,n,a) for( int i=(n);i>=(a);i--)
- #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
- #define memset(a,i,b) memset((a),(i),sizeof (b))
- #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
- #define endl "\n"
- #define lowbit(m) ((-m)&(m))
- #define dbug(y) cout<<(y)<<"\n"
- #define dbug2(a,b) cout<<(a)<<" "<<(b)<<"\n"
- #define dbug3(a,b,c) cout<<(a)<<" "<<(b)<<" "<<(c)<<"\n"
- #define dbug4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<"\n"
- #define tulun int e[N],ne[N],h[N],w[N],idx
- #define T_solve() int T;cin>>T;while(T--)solve()
- #define pi 3.14159265358979323846
- using namespace std;
- typedef long long LL;
- typedef unsigned long long ULL;
- typedef pair<int,int> PII;
- typedef pair<long long,long long> PLL;
- typedef double dob;
- const int N=1e2+10,mod=1e6+3,base=131;
- int n,m;
- int a[N][N];
- int d[N][N];
- int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
- void bfs()
- {
- queue
q; - q.push({1,1});
- d[1][1]=1;
- while(q.size())
- {
- auto t=q.front();
- q.pop();
- int x=t.first,y=t.second;
- rep1(i,0,4)
- {
- int xx=x+dx[i];
- int yy=y+dy[i];
- if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&!a[xx][yy]&&!d[xx][yy])
- {
- d[xx][yy]=d[x][y]+1;
- q.push({xx,yy});
- }
- }
- }
- dbug(d[n][m]-1);
- }
- signed main()
- {
- quick_cin();
- cin>>n>>m;
- rep2(i,1,n)
- rep2(j,1,m)cin>>a[i][j];
- bfs();
- return 0;
- }
6,dijkstra算法;
注意continue的使用;
- #pragma GCC optimize(3)
- #include
- #define rep1(i,a,n) for( int i=(a);i<(n);++i)
- #define rep2(i,a,n) for( int i=(a);i<=(n);++i)
- #define per1(i,n,a) for( int i=(n);i>(a);i--)
- #define per2(i,n,a) for( int i=(n);i>=(a);i--)
- #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
- #define memset(a,i,b) memset((a),(i),sizeof (b))
- #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
- #define endl "\n"
- #define lowbit(m) ((-m)&(m))
- #define dbug(y) cout<<(y)<<"\n"
- #define dbug2(a,b) cout<<(a)<<" "<<(b)<<"\n"
- #define dbug3(a,b,c) cout<<(a)<<" "<<(b)<<" "<<(c)<<"\n"
- #define dbug4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<"\n"
- #define tulun int e[N],ne[N],h[N],w[N],idx
- #define T_solve() int T;cin>>T;while(T--)solve()
- #define pi 3.14159265358979323846
- using namespace std;
- typedef long long LL;
- typedef unsigned long long ULL;
- typedef pair<int,int> PII;
- typedef pair<long long,long long> PLL;
- typedef double dob;
- const int N=1e5+10,mod=1e6+3,base=131;
- tulun;
- void add(int a,int b,int c)
- {
- w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
- }
- int n,m;
- int dist[N];
- int st[N];
- void dijkstra()
- {
- memset(dist,0x3f,dist);
- priority_queue
,greater>q; - q.push({0,1});
- dist[1]=0;
- while(q.size())
- {
- auto t=q.top();
- q.pop();
- int jl=t.first,ver=t.second;
- if(st[ver])continue;
- st[ver]=1;
- for(int i=h[ver];~i;i=ne[i])
- {
- int j=e[i];
- if(dist[j]>w[i]+jl)
- {
- dist[j]=w[i]+jl;
- q.push({dist[j],j});
- }
- }
- }
- if(dist[n]==0x3f3f3f3f)dbug(-1);
- else dbug(dist[n]);
- }
- signed main()
- {
- quick_cin();
- memset(h,-1,h);
- cin>>n>>m;
- rep2(i,1,m)
- {
- int a,b,c;
- cin>>a>>b>>c;
- add(a,b,c);
- }
- dijkstra();
- return 0;
- }
7,floyed算法;
适用数据范围小的最短路,可以求负权边;
注意初始化的方法,和dp类似,注意含义;
- #pragma GCC optimize(3)
- #include
- #define rep1(i,a,n) for( int i=(a);i<(n);++i)
- #define rep2(i,a,n) for( int i=(a);i<=(n);++i)
- #define per1(i,n,a) for( int i=(n);i>(a);i--)
- #define per2(i,n,a) for( int i=(n);i>=(a);i--)
- #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
- #define memset(a,i,b) memset((a),(i),sizeof (b))
- #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
- #define endl "\n"
- #define lowbit(m) ((-m)&(m))
- #define dbug(y) cout<<(y)<<"\n"
- #define dbug2(a,b) cout<<(a)<<" "<<(b)<<"\n"
- #define dbug3(a,b,c) cout<<(a)<<" "<<(b)<<" "<<(c)<<"\n"
- #define dbug4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<"\n"
- #define tulun int e[N],ne[N],h[N],w[N],idx
- #define T_solve() int T;cin>>T;while(T--)solve()
- #define pi 3.14159265358979323846
- using namespace std;
- typedef long long LL;
- typedef unsigned long long ULL;
- typedef pair<int,int> PII;
- typedef pair<long long,long long> PLL;
- typedef double dob;
- const int N=1e3+10,mod=1e6+3,base=131;
- int d[N][N];
- int n,m,k;
- #define inf 0x3f3f3f3f
- void floyed()
- {
- rep2(k,1,n)
- rep2(i,1,n)
- rep2(j,1,n)
- d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
- }
- signed main()
- {
- quick_cin();
- cin>>n>>m>>k;
- rep2(i,1,n)
- rep2(j,1,m)
- {
- if(i==j)d[i][j]=0;
- else d[i][j]=inf;
- }
- rep2(i,1,m)
- {
- int a,b,c;
- cin>>a>>b>>c;
- d[a][b]=min(d[a][b],c);
- }
- floyed();
- rep2(i,1,k)
- {
- int a,b;
- cin>>a>>b;
- if(d[a][b]>inf/2)dbug("impossible");
- else dbug(d[a][b]);
- }
- return 0;
- }
-
相关阅读:
1.django简介及安装
所有字母异位词
js分割字符串
【无人机通信优化】基于粒子群算法的多跳无线网络部署优化附matlab代码
(亲测完全可行)charles抓包夜神模拟器保姆级教程
Java: Java中接口和抽象类的区别以及应用场景
72道Java线程面试题,一题一答案,不搞花里胡哨
[吴恩达机器学习课程笔记] week four强化学习
ubuntu下yolov5 tensorrt模型部署
力扣 轮转数组三段逆置法和三段拷贝法(C语言)
-
原文地址:https://blog.csdn.net/aidaqiudeaichao/article/details/128017715