kmpの板子:
namespace kmp{
void getNext(string t, vector<int>& next) {
int i = 0, j = -1;
next.resize(t.size());
next[0] = -1;
while (i < t.size() - 1) {
if (j == -1 || t[i] == t[j]) {
next[++i] = ++j;
} else {
j = next[j];
}
}
}
int kmp(string s, string t, int pos) {
int i = pos, j = 0;
vector<int> next;
getNext(t, next);
while (i < s.size() && j < t.size()) {
if (s[i] == t[j]) {
i++;
j++;
} else {
j = next[j];
}
if (j == -1) {
j++;
i++;
}
}
return j >= t.size() ? i - t.size() : -1;
}
}链式前向星模板:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10,M=1e6+10;
int n,m;
struct node{
int to,dis,next;
} edge[M];
int cnt;
int head[N];
void push(int from,int to,int dis){
cnt++;
edge[cnt].dis=dis;
edge[cnt].to=to;
edge[cnt].next=head[from];
head[from]=cnt;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);
cin>>n>>m;
int x,y,d;
for(int i=1;i<=m;i++){
cin>>x>>y>>d;
push(x,y,d);
//push(y,x,d);
}
/*for(int i=1;i<=m;i++){
cin>>x>>y;
push(x,y,1);
//push(y,x,1);
}*/
for(int i=1;i<=n;i++){
for(int j=head[i];j!=0;j=edge[j].next){
cout<<"from:"<<i<<" to:"<<edge[j].to<<" distance:"<<edge[j].dis<<"\n";
}
}
}