跳到正文

TCIC_4294967296

1155762

Florr ID:Segment | Lvl.66 | 约80M | 0U | 0S | 已AFO

在洛谷打开
资料、奖项与版本

资料

编号
1155762
抓取

奖项

2024 CSP-J
一等奖
2024 CSP-S
一等奖

kmpの板子:

C++
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;
	}
}

链式前向星模板:

C++
#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";
		}
	}
}

还没有收录这位用户的文章。