跳到正文

个人记录

【考前复习】个人团队文件保存内容登记

yanzie约 13 分钟
原文
文章信息与版本

文章信息

发布
2025年11月30日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 13 分钟
获赞
1

版本

  1. v7当前
  2. v6
  3. v5
  4. v4
更早的 3 个版本
  1. v3
  2. v2
  3. v1

描述内容自团队:https://www.luogu.com.cn/team/103072#main

luogu.me记得手动更新
专栏双站快速跳转:专栏/luogu.me专栏

该文章仅用于个人算法模板保存内容和快速检索与资源共享该文章仅用于 个人算法模板保存内容和快速检索与资源共享

该团队仅用于个人算法模板保存与共享该团队仅用于 个人算法模板保存与共享

内容登记:

!最小生成树模板:

课程内容

https://xinyoudui.com/courses/1708/all/#/pages/63733
https://vjudge.net/contest/768814
https://vjudge.net/contest/769118

官方3份:

Kruskal构造树,Kruskal,prim;

个人2份:

  • Kruskal,prim;

!字符串哈希:

课程内容概览

https://vjudge.net/contest/770555#rank
https://xinyoudui.com/courses/1708/all/#/pages/60219
https://vjudge.net/contest/770558#overview

个人2份:

  • 字符串哈希(自然溢出取模型)
C++
#include<bits/stdc++.h>
using namespace std;
string s,t,ans;
int n,m;
unsigned long long hs,ht,x=1e3+7;
int main(){
	cin>>n>>m;
	cin>>s>>t;
	for(int i=0;i<t.size();i++){
		hs=hs*x+t[i];
		to*=x;
	}
	int j=0;
	for(int i=0;i<t.size();i++){
		j++;
		ht=ht*x+t[i];
		if(i>=size()){
			ht-=to*t[i-s.size()];
		}
		if(ht==hs){
			ans++;
			h=c=0;
			continue;
		}
	}
}
  • 双哈希:
C++
#include<bits/stdc++.h>
#define ULL unsigned long long
using namespace std;
const ULL N=2e6+5,x1=233,x2=1007,mod=1e9+7;
pair<ULL,ULL> h[N];
ULL H1(string s){//大质数哈希 
	ULL ret=0;
	for(int i=0; i<s.length(); i++) 
		ret=(ret*x1+s[i])%mod; 
	return ret;
}
ULL H2(string s){//自然溢出哈希 
	ULL ret=0;
	for(int i=0;i<s.length();i++) 
		ret=ret*x2+s[i]; 
	return ret;
}
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;i++){	 
		string s;
		cin>>s;
		h[i].first=H1(s);
		h[i].second=H2(s);
	}
	sort(h,h+n);	
	cout<<unique(h,h+n)-h;
}

个人电子3份:

  • 典型例题不重叠子串数:KMP2
C++
#include<bits/stdc++.h>
using namespace std;
string s,t;
int n,m,x=1e3+7;
unsigned long long hs,ht,to=1,ans;
int main(){
	cin>>n>>m;
	cin>>s>>t;
	for(int i=0;i<t.size();i++){
		hs=hs*x+t[i];
		to*=x;
	}
	int j=0;
	for(int i=0;i<s.size();i++){
		j++;
		ht=ht*x+s[i];
		if(j>t.size()){
			ht-=to*s[i-t.size()];
		}
		if(ht==hs){
			ans++;
			ht=0,j=0;
		}
	}
    cout<<ans;
}
C++
#include<bits/stdc++.h>
using namespace std;
string s;
int f[5000005];
unsigned long long h1,h2,to=1,x=131,ans;
int main(){
	cin>>s;
    s=' '+s;
	for(int i=1;i<s.size();i++){
		h1=h1*x+s[i];
        h2+=to*s[i];
        to*=x;
        if(h1==h2){
            f[i]=f[i/2]+1;
            ans+=f[i];
        }
	}
    cout<<ans;
}
C++
#include<bits/stdc++.h>
using namespace std;
string t;
long long n,cnt;
unsigned long long x1=4e9+7,x2=4e9+7,mod=9223372036854775707,ans;
pair<unsigned long long,unsigned long long>h[2000005];
unsigned long long H1(string s){//大质数哈希 
	unsigned long long ret=0;
	for(int i=0;i<s.length();i++) 
		ret=ret*x1+s[i],ret%=mod; 
	return ret;
}
unsigned long long H2(string s){//自然溢出哈希 
	unsigned long long ret=0;
	for(int i=0;i<s.length();i++) 
		ret=ret*x2+s[i]; 
	return ret;
}
bool cmp(pair<unsigned long long,unsigned long long>h1,pair<unsigned long long,unsigned long long>h2){
    if(h1.first==h2.first){
        return h1.second<h2.second;
    }
    return h1.first<h2.first;
}
int main(){
	cin>>n;
	while(n--){
		cin>>t;
        h[++cnt].first=H1(t);
        h[cnt].second=H2(t);
        // cout<<h[cnt].first<<" "<<h[cnt].second<<"\n";
	}
    sort(h+1,h+cnt+1,cmp);
    for(int i=1;i<cnt;i++){
        // cout<<h[i].first<<" "<<h[i].second<<"\n";
        if(h[i].first!=h[i+1].first&&h[i].second!=h[i+1].second){
            ans++;
        }
    }
    cout<<ans+1;
}

官方2组:

  • 字符串哈希-秦九韶算法:团队文件;
  • 哈希讲义1组(2份)

官方电子1份

!字典树

课程内容:

官方2组

  • 字典树讲义
  • 字典树PPT
    代码模板见 PPT 或 讲义 或 偷来的别人模板(陶羽轩’s)
官方电子0份
  • ?
个人0份
  • ?

个人电子?份

  • P8306别问我为什么代码和有些人一样。。。

!计数dp

课程内容:

\

!树状数组

课程内容:

官方1组

  • PPT

个人1份

  • 树状数组模板(团队文件)

个人电子?份

  • 见提交记录

    !模板:Title

课程内容:

  • xinyoudui.com/?
  • vjudge.net/?

官方2组

  • ?

官方电子?份

  • ?

个人?份

  • ?

个人电子?份

  • ?

----------春季内容-----------

★【考试技巧重点】STL与常数优化(考前可复习)

快读快写

  • 好吧,其实约等于快读慢写
C++
//P10815 差一点AC代码
#include<bits/stdc++.h>
using namespace std;
int n;
long long sum;
inline void read(int &x){
	bool neg=0;
    x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
    	if(ch=='-')neg=true;
    	ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
    	if(neg)x=x*10+('0'-ch);
        else x=x*10+(ch-'0');;
    	ch=getchar();
    }
}
inline void write(int x){
	bool neg = false;
	if (x < 0) {
	   neg = true;
	   putchar('-');
	}
	static int sta[40];
	int top = 0;
	do {
		sta[top++] = x % 10;
		x /= 10;
	} while (x);
	if (neg)
	    while (top) putchar('0' - sta[--top]);
	else
	    while (top) putchar('0' + sta[--top]);
}
int main(){
    ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	read(n);
    for(int i=1;i<=n;i++){
        int x;
        read(x);
        // cout<<x<<'\n';
        sum+=x;
    }
    write(sum);
}
  • 小灶:在有些题getchar()过不了,可以用getchar_unlocked()如P10815,但不建议,有的编译器不行
  • P10815 AC代码(其实就是在上面代码的基础上 开头加了一个#define getchar() getchar_unlocked())

【考试技巧重点】★常见常数优化(卡常)

  • 关闭同步流
C++
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);//0可以用null false等替代
  • ++i 代替 i++
C++
//	for等使用++i会比i++快,原因不说了,约能快0.5X
//例
for(int i=1;i<=n;++i){
    read(n);
}
  • 使用用位运算代替四则运算
    例:
C++
x*10 => (x<<3)+(x<<2);
x*8 => x<<3;
x/2 => x>>1;
  • emplace()
    emplace()代替push
    emplace_back()代替push_back() 可以加速

内容未完待续,团队文件里放了STL和常数优化PPT、代码,这些技巧很重要,而且不难

其它

最长上升子序列

快速幂qpow

C++
#include<iostream>
using namespace std;
long long a,b,p;
long long qp(long long a,long long b){
	long long ans=1,t=a;
    while(b>0){
        if(b&1){
            ans*=t;
            ans%=p;
        }
        t*=t;
        t%=p;
        b>>=1;
    }
    return ans;
} 
int main(){
	cin>>a>>b>>p;
	long long ans=qp(a,b);
	ans=ans%p;
	printf("%lld^%lld mod %lld=%lld",a,b,p,ans);
	return 0;
}
原文