递归求表达式的值
int calc(int l,int r) {
for(int i=r,j=0;i>=l;--i) {
if(s[i]=='(')++j;
if(s[i]==')')--j;
if(!j && s[i]=='+') return calc(l,i-1)+calc(i+1,r);
if(!j && s[i]=='-') return calc(l,i-1)-calc(i+1,r);
}
for(int i=r,j=0;i>=l;--i) {
if(s[i]=='(')++j;
if(s[i]==')')--j;
if(!j && s[i]=='*') return calc(l,i-1)*calc(i+1,r);
if(!j && s[i]=='/') return calc(l,i-1)/calc(i+1,r);
}
//加减法优先度低
if(s[l]=='(' && s[r]==')') return calc(l+1,r-1);
//括号内
int ans = 0;
for(int i=l;i<=r;++i) ans=ans*10+s[i]-'0';
return ans;
//单个数。
}
KMP, 要求输出位置和 border 数组
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+15;
char s1[maxn], s2[maxn];
int len1, len2;
int nxt[maxn], f[maxn];
int main()
{
scanf("%s%s", s1+1,s2+1);
len1=strlen(s1+1), len2=strlen(s2+1);
for(int i=2,j=0;i<=len2;++i) {
while(j&&s2[j+1]!=s2[i]) j=nxt[j];
if(s2[j+1]==s2[i]) ++j;
nxt[i]=j;
}
for(int i=1,j=0;i<=len1;++i) {
while(j&&(j==len2||s2[j+1]!=s1[i])) j=nxt[j];
if(s2[j+1]==s1[i]) ++j;
f[i]=j;
}
for(int i=1;i<=len1;++i) if(f[i]==len2) cout<<i-len2+1<<'
';
for(int i=1;i<=len2;++i) cout<<nxt[i]<<' ';
return 0;
}
倍增 LCA
#include<bits/stdc++.h>
using namespace std;
const int N = 500003;
int n,m,s;
int ct, hd[N], nt[N<<1], vr[N<<1];
int f[23][N], dep[N];
void dfs1(int x,int fa) {
f[0][x] = fa;
for(int i=hd[x];i;i=nt[i]) {
int y = vr[i];
if(y==fa) continue;
dep[y] = dep[x]+1;
dfs1(y,x);
}
}
int lca(int x,int y) {
if(dep[x]>dep[y]) swap(x,y);
for(int k=22;k>=0;--k)
if(dep[f[k][y]] >= dep[x]) y=f[k][y];
if(x==y) return x;
for(int k=22;k>=0;--k)
if(f[k][x] != f[k][y]) x=f[k][x], y=f[k][y];
return f[0][x];
}
void ad(int x,int y) {
vr[++ct]=y, nt[ct]=hd[x], hd[x]=ct;
}
int main() {
cin>>n>>m>>s;
for(int i=1;i<n;++i) {int x,y;scanf("%d%d",&x,&y);ad(x,y);ad(y,x);}
dep[s]=1; // important
dfs1(s,0);
for(int k=1;k<=22;++k)
for(int i=1;i<=n;++i)
f[k][i] = f[k-1][f[k-1][i]];
while(m--) {
int x,y; scanf("%d%d",&x,&y); cout<<lca(x,y)<<'
';
}
return 0;
}
判负环
bool SPFA() {
queue<int>q;
for(int i=1;i<=n;++i) {
q.push(i);
inq[i] = 1;
dist[i] = 0;
}
memset(cnt,0,sizeof cnt);
while(q.size()) {
int x=q.front(); q.pop(); inq[x]=0;
for(int i=hd[x];i;i=nt[i]) {
int y = vr[i];
if(dist[y]>dist[x]+w[i]) {
dist[y] = dist[x]+w[i];
if(++cnt[y]>n) return true;
if(!inq[y]) {inq[y]=1, q.push(y);}
}
}
}
return false;
}
割边
int dfntot, dfn[N], low[N];
bool bridge[M*2];
void tarjan(int x,int in_edge) {
dfn[x] = low[x] = ++dfntot;
for(int i=hd[0][x];i;i=nt[i]) {
int y = vr[i];
if(!dfn[y]) {
tarjan(y,i);
low[x] = min(low[x],low[y]);
if(low[y]>dfn[x]) bridge[i]=bridge[i^1]= true;
} else if(i!=(in_edge^1))low[x]= min(low[x],dfn[y]);
}
}
割点
int dfntot, dfn[N], low[N], dcccnt;
vector<int>dcc[N];
int sta[N], tp;
bool cut[N];
int root;
void tarjan(int x) {
dfn[x] = low[x] = ++dfntot;
if(x==root && hd[x]==0) {
dcc[++dcccnt].clear();
dcc[dcccnt].push_back(x);
return;
}
sta[++tp] = x;
int flag = 0;
for(int i=hd[x];i;i=nt[i]) {
int y = vr[i];
if(!dfn[y]) {
tarjan(y);
low[x] = min(low[x],low[y]);
if(low[y]>=dfn[x]) {
++flag;
if(x!=1 || flag>1) cut[x]=true;
dcc[++dcccnt].clear();
int z;
do{
z = sta[tp--];
dcc[dcccnt].push_back(z);
} while(z!=y);
dcc[dcccnt].push_back(x);
}
} else low[x] = min(low[x],dfn[y]);
}
}