1.一维数组的前缀和
#include <iostream>
using namespace std;
const int N = 100010;
int main() {
int n,m;
scanf("%d%d",&n,&m);
int a[N],q[N];
for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
for(int i = 1;i <= n;i++) q[i] = q[i - 1] + a[i];
while(m--) {
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",q[r] - q[l - 1]);
}
return 0;
}
2.最长连续不重复子序列
#include <iostream>
using namespace std;
const int N = 100010;
int a[N],s[N];
int main() {
int n;
scanf("%d",&n);
for(int i = 0;i < n;i++) scanf("%d",&a[i]);
int res = -1;
for(int i = 0,j = 0;i < n;i++) {
s[a[i]]++;
while(j < i && s[a[i]] > 1) s[a[j++]]--;
res = max(res,i - j + 1);
}
printf("%d\n",res);
return 0;
}
3.并查集--合并集合
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int pre[N];
int find(int x) {
if(pre[x] != x) pre[x] = find(pre[x]);
return pre[x];
}
int main() {
int n,m;
cin>>n>>m;
for(int i = 0;i < n;i++) pre[i] = i;
while(m--) {
char op[2];
int a,b;
cin>>op>>a>>b;
if(*op == 'M') pre[find(a)] = find(b);
else {
if(find(a) == find(b)) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
return 0;
}
4.DFS--n皇后问题
#include <iostream>
using namespace std;
const int N = 1005;
bool col[N],dg[N],udg[N]; //其中dg表示的是对角线,udg表示的是反对角线
char a[N][N]; //表示的是每一位的元素的值
int n;
void dfs(int u) {
if(n == u) {
for(int i = 0;i < n;i++) cout<<a[i]<<endl;
cout<<endl;
return;
}
int x = u;
for(int y = 0;y < n;y++) {
//剪枝,缩小搜索的范围
if(col[y] == false && dg[y - x + n] == false && udg[y + x] == false) {
col[y] = true;
dg[y - x + n] = true;
udg[y + x] = true;
a[x][y] = 'Q';
dfs(u + 1);
a[x][y] = '.';
col[y] = dg[y - x + n] = udg[y + x] = false;
}
}
}
int main() {
scanf("%d",&n);
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
a[i][j] = '.';
}
}
dfs(0);
return 0;
}
5.BFS--走迷宫问题
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 1e2 + 7;
int g[N][N], d[N][N];
int n, m;
int bfs() {
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
queue <PII> q;
for (auto &v : d)
for (auto &x : v) {
x = - 1;
}
d[0][0] = 0;
q.push({0, 0});
while (!q.empty()) {
auto t = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int x = t.first + dx[i], y = t.second + dy[i];
if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1) {
d[x][y] = d[t.first][t.second] + 1;
q.push({x, y});
}
}
}
return d[n - 1][m - 1];
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> g[i][j];
}
}
cout << bfs() << endl;
return 0;
}
6.BFS--八数码问题
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
int fact[9];
bool vis[362880];
int permutation_hash(char s[]) //求长度为9的字符串某种排列的哈希值
{
int ans = 0;
for(int i = 0; i < 9; i ++)
{
int d = 0;
for(int j = 0; j < i; j ++)
if(s[j] > s[i]) d ++; //求s[i]与其前面的字符组成的逆序对个数
ans += d * fact[i];
}
return ans;
}
typedef struct{
char s[10];
int step;
int k; //'x'在第k位
}Point;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = { 0,-1, 0, 1};
int bfs(Point p)
{
vis[permutation_hash(p.s)] = true;
queue<Point> q;
q.push(p);
while(!q.empty())
{
p = q.front();
q.pop();
/*
printf("%d ",p.step); //print调试法
puts(p.s);
*/
if(!strcmp(p.s , "12345678x")) return p.step;
int x = p.k / 3; //'x'的行数
int y = p.k % 3; //'x'的列数
Point next;
next.step = p.step + 1;
for(int i = 0; i < 4; i ++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(nx >= 0 && nx <= 2 && ny >= 0 && ny <= 2)
{
next.k = nx * 3 + ny; //求出'x'在字符串中的的新位置
strcpy(next.s, p.s);
next.s[9] = 0;
next.s[p.k] = p.s[next.k]; //先用即将和'x'交换的字符覆盖'x'之前的位置
next.s[next.k] = 'x'; //再给'x'的新位置赋值'x'
int hash = permutation_hash(next.s);
if(!vis[hash])
{
vis[hash] = true;
q.push(next);
}
}
}
}
return -1;
}
int main()
{
fact[0] = 1;
for(int i = 1; i < 9; i ++) fact[i] = fact[i - 1] * i; //预处理fact[i] = i!
char c[2],str[10];
Point start;
for(int i = 0; i < 9; i ++)
{
scanf("%s",&c);
if(c[0] == 'x') start.k = i;
start.s[i] = c[0];
}
start.s[9] = 0;
start.step = 0;
printf("%d",bfs(start));
return 0;
}