图的表示
昨天小明回复了我的消息,那么今天我要去小明家,好像情感发展进度很快呀,哈哈哈!现在我手头有一份地图,但是我不关心其他图例和注记,只想看我家和他家的连通关系,那么我家和他家都可以视为点,路径可以视为线(有时是桥),有时甚至有长短,有方式,比如坐船去和做飞机去,路线不一样,花费也不一样。
我可以用邻接矩阵来记录,就是一个表,可以记录有向图(存在单行线),有的时候也会有环,似乎出租车司机欺负外地人的时候特别喜欢见到环,哈哈!
当然,我也可以用邻接表来记录,每个家拥有一本通讯录,可以知道可以给谁打电话(和谁可以互相连接),通过区号也可以知道是不是长途电话,打电话的花费是多少。
想要找到小明家就需要搜索图,可以从我家开始一直往北找,找不到在从我家一直往南找,等等,如果地图很大,找起来可能会比较久,我们可能会搞乱,但是好在优秀的程序不会乱,通过递归+前/中/后序遍历就可以找到,我们称之为深度优先搜索DFS(通常应用于走迷宫)。
int next[8][2]= {{-2,-1},
{-2,1},
{-1,-2},
{-1,2},
{1,-2},
{1,2},
{2,-1},
{2,1}}; //八个方向,有顺序要求
if(cot==p*q) //边界条件
{
win=1;
return;
}
for(int i=0;i<=7;i++)
{
int tx=next[i][0]+x; //上表起作用了,枚举下一步
int ty=next[i][1]+y;
if(tx<1||tx>p||ty<1||ty>q) //越界返回
continue;
if(flag[tx][ty]==0&&!win)
{
flag[tx][ty]=1; //标记该步已经走了
dfs(tx,ty,cot+1); //深入下一步
flag[tx][ty]=0; //来到这里说明上一步失败了,取消该步访问,即回溯
}
}
但很多时候,我们知道小明家应该不会离我家很远,那我一圈一圈往外找,逐渐扩大搜索半径也是极好的,于是我们用queue来维护这种关系,就可以实现了,我们称之为广度优先搜索(通常应用于?)。
struct node
{
int x, step;
};
queue<node> Q;
while(!Q.empty())
{
node tmp;
tmp=Q.front();
Q.pop();
x=tmp.x;
stp=tmp.step;
}
if((!vis[tep])&&(tep!=x)&&(prime[tep]))
Q.push(temp);
高高兴兴地到了小明家,没想到他给我准备了一道难题:
有n个小朋友,每个小朋友手里有一些糖块,现在这些小朋友排成一排,编号是由1到n。现在给出m个数,能不能唯一的确定一对值l和r(l <= r),使得这m个数刚好是第l个小朋友到第r个小朋友手里的糖块数?
思路解析:典型的KMP问题,只要将模板写下,然后根据题意,写出细节就好了。
#include<stdio.h>
int a[100000009],b[100000009];
int next[100000010],n,m;
void Next()
{
next[0]=-1;
for(int j=1;j<m;j++)
{
int i=next[j-1];
while(b[j]!=b[i+1]&&i>=0)
{
i=next[i];
}
if(b[j]==b[i+1])
next[j]=i+1;
else
next[j]=-1;
}
}
int KMP(int a[],int b[])
{
Next();
int p=0,s=0;
while(p<m&&s<n)
{
if(a[s]==b[p])
{
s++;
p++;
}
else
{
if(p==0)
s++;
else
p=next[p-1]+1;
}
}
if(p<m)
return -1;
else
return s-m+1;
}
int main()
{
int i, k, t;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(i=0;i<m;i++)
scanf("%d",&b[i]);
k = KMP(a,b);
if(k!=-1)
{
t = KMP(a+k,b);
if(t==-1)
printf("%d %d\n", k, k+m-1);
else
printf("-1\n");
}
else
printf("-1\n");
}
return 0;//在竞赛中,请保持返回0,以免评测系统以为你的代码运行错误了。
}
给出一个模板,包括 getF() 和 getMatch()
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 1000003
char F[maxn],T[maxn],P[maxn];
int m,n,d[maxn];
int ans=0,cnt=0;
void getF(char *P,int m)
{
F[1]=0;
for(int i=2;i<=m;++i)
{
int k=F[i-1];
if(P[k+1]==P[i])
++k;
else{
while(k>0&&P[k+1]!=P[i])
k=F[k];
if(P[k+1]==P[i])++k;
}
F[i]=k;
}
}
void getMatch(char*P,int m,char*T,int n)
{
getF(P,m);
int k=0;
for(int i=1;i<=n;++i)
{
while(k&&T[i]!=P[k+1]) k=F[k];
if(T[i]==P[k+1]) ++k;
if(k==m)
{
//printf("matched at %d\n",i-m+1);
d[cnt++]=i-m+1;
ans++;
k=F[k];
//i+=m-1;
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s %s",T+1,P+1);
n=strlen(&T[1]);
m=strlen(&P[1]);
getMatch(P,m,T,n);
if(ans)printf("%d\n",ans);
else printf("Not Found\n");
for(int i=0;i<cnt;i++)
printf("%d ",d[i]);
if(n>0)printf("\n");
ans=0;cnt=0;
}
}
终于可以愉快地玩耍了,可是小明的爸爸妈妈都在家,我们玩的话会太吵,于是我们去了楼下的花园。玩什么好呢,玩石子吧,这是博弈的故事了。玩了好多局之后,我们发现好像有必胜和必输的方法,于是我们去推理这种方法,有时和取模有关,有时和奇偶有关,有时和异或有关,有时似乎就是凭运气。于是我们的游戏变成了:制定游戏规则,猜谁会赢。
有时,题目并不需要SG的结果,或者说与SG裸题有差异,这时我们依然可以用SG函数的思想打表,帮助我们寻找规律。
电影:美丽心灵:纳什
这里附上黄金分割:g = 1.618 = ( sqrt(5) + 1 ) / 2;
以及圆周率π:pi = acos(-1.0);
Sprague-Grundy函数
对于我们来说,SG函数与“游戏的和”的概念不是让我们去组合、制造稀奇古怪的游戏,而是把遇到的看上去有些复杂的游戏试图分成若干个子游 戏,对于每个比原游戏简化很多的子游戏找出它的SG函数,然后全部异或起来就得到了原游戏的SG函数,就可以解决原游戏了。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
int len;
int canchoose[105];
int num;
int SG[10010];
int getSG(int x)
{
if(SG[x]!=-1) return SG[x];
bool flag[105];
memset(flag,false,sizeof(flag));
for(int i=0;i<len;i++)
{
if(canchoose[i]>x) break;
int tmp=x-canchoose[i];
if(SG[tmp]==-1) SG[tmp]=getSG(tmp);
flag[SG[tmp]]=true;
}
for(int i=0;;i++) if(!flag[i]) return (SG[x]=i);
}
int main() {
// freopen("in.txt","r",stdin);
while(cin>>len&&len)
{
for(int i=0;i<len;i++)
cin>>canchoose[i];
sort(canchoose,canchoose+len);
int T;
memset(SG,-1,sizeof(SG));
cin>>T;
while(T--)
{
int dui;
cin>>dui;
int ans=0;
for(int i=0;i<dui;i++)
{
cin>>num;
ans^=getSG(num);
}
cout<<(ans?'W':'L');
}
cout<<endl;
}
return 0;
}
图
坐出租车
不是所有牛奶都叫特仑苏,不是所有图都叫有向图
<vector“>”push_back 动态数组处理邻接表
路径的长度:权值相加
最短路径:1、不一定存在INF
2、不一定唯一
聪明的弗洛伊德等数学家给出了多种方法。
相关推荐:
离散数学
组合数学
线性代数
树
把路上的树拔起来在倒过来了,但是还是树,这种只关心连接关系的关系叫做拓扑关系。我们可以发现树枝之间没有交叉成环之类的,而且树枝一般是一分二的,在这里,我们把这样的树成为二叉树,同样的,聪明的你,一定知道,N叉树是怎么样的了。
BFS查找&插入
#include<bits/stdc++.h>
using namespace std;
const int SIZE = 50010;
int f[SIZE][20], d[SIZE], dist[SIZE];
int ver[2 * SIZE], Next[2 * SIZE], edge[2 * SIZE], head[SIZE];
int T, n, m, tot, t;
queue<int> q;
void add(int x, int y, int z)
{
ver[++tot] = y;
edge[tot] = z;
Next[tot] = head[x];
head[x] = tot;
}
// 预处理
void bfs()
{
q.push(1);
d[1] = 1;
while (q.size())
{
int x = q.front();
q.pop();
for (int i = head[x]; i; i = Next[i])
{
int y = ver[i];
if (d[y]) continue;
d[y] = d[x] + 1;
dist[y] = dist[x] + edge[i];
f[y][0] = x;
for (int j = 1; j <= t; j++)
f[y][j] = f[f[y][j - 1]][j - 1];
q.push(y);
}
}
}
// 回答一个询问
int lca(int x, int y)
{
if (d[x] > d[y]) swap(x, y);
for (int i = t; i >= 0; i--)
if (d[f[y][i]] >= d[x]) y = f[y][i];
if (x == y) return x;
for (int i = t; i >= 0; i--)
if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}
int main()
{
cin >> T;
while (T--)
{
cin >> n >> m;
t = (int)(log(n) / log(2)) + 1;
// 清空
for (int i = 1; i <= n; i++) head[i] = d[i] = 0;
tot = 0;
// 读入一棵树
for (int i = 1; i < n; i++)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
add(x, y, z), add(y, x, z);
}
bfs();
// 回答问题
for (int i = 1; i <= m; i++)
{
int x, y;
scanf("%d%d", &x, &y);
printf("%d\n", dist[x]+dist[y]-2*dist[lca(x, y)]);
}
}
}
最近公共祖先:向上标记法
贪心问题:我要我要,我全都要!欧巴,赞多利!
欧巴,赞多利!
动态规划:考虑全局最优?
区间DP:末减初
查并集:把连通者放在同一个猪圈里。
写不动了,先这样吧!