其他:
洛谷模板:a,b两个字符串,求b串在a串中出现的位置
#include<iostream> #include<cstdio> #include<cstring> using namespace std; char s1[1000009],s2[1001]; int len1,len2,Next[1001]; int main(){ scanf("%s%s",s1+1,s2+1); len1=strlen(s1+1);len2=strlen(s2+1); for(int i=2,k=0;i<=len2;i++){ for(;s2[i]!=s2[k+1]&&k>0;k=Next[k]); if(s2[i]==s2[k+1])Next[i]=++k; } for(int i=1,k=0;i<=len1;i++){ for(;s1[i]!=s2[k+1]&&k>0;k=Next[k]); if(s1[i]==s2[k+1])++k; if(k==len2)printf("%d ",i-len2+1),k=Next[k]; } for(int i=1;i<=len2;i++)printf("%d ",Next[i]); return 0; }
洛谷模板:n个字符串求有几个不同的串
#include<iostream> #include<algorithm> #include<cstdio> #include<set> #include<cstring> using namespace std; //#define mod 1e9+7//不能这样宏定义 const int mod=1e9+7; #define D 131 #define M 2333+7 long long n,g[M],f[M]; string s; set<long long>t; void predeal(int x){ f[0]=s[0]; for(int i=1;i<=x;i++) f[i]=(1LL*f[i-1]*D+s[i-1])%mod; g[0]=1; for(int i=1;i<=x;i++) g[i]=1ll*g[i-1]*D%mod; } int Hash(int l,int r){ long long a=f[r],b=1LL*f[l-1]*g[r-l+1]%mod; return (a-b+mod)%mod; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++){ cin>>s; long long len=s.length(); predeal(len); long long k=Hash(1,len); t.insert(k); } printf("%lld ",t.size()); return 0; }
洛谷模板:最长回文串
#include<cstdio> #include<cstring> #include<iostream> using namespace std; const int maxn=1e7+5; char s[maxn*2],str[maxn*2]; int Len[maxn*2],len; void getstr() { int k=0; str[k++]='$'; for(int i=0;i<len;i++) str[k++]='#', str[k++]=s[i]; str[k++]='#'; len=k; } void Manacher() { getstr(); int mx=0,id; for(int i=1;i<len;i++) { if(mx>i) Len[i]=min(Len[2*id-i],mx-i); else Len[i]=1; while(str[i+Len[i]]==str[i-Len[i]]) Len[i]++; if(Len[i]+i>mx) mx=Len[i]+i,id=i; } } int main() { scanf("%s",&s); len=strlen(s); Manacher(); int ans=1; for(int i=1;i<len;i++) ans=max(ans,Len[i]); printf("%d ",ans-1); return 0; }
洛谷模板:快速排序
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,a[100009],tmp[100009]; void merge_sort(int l,int r){ if(l==r)return; int mid=(l+r)>>1; merge_sort(l,mid); merge_sort(mid+1,r); int ii=l,jj=mid+1,k=l; while(ii<=mid&&jj<=r){ if(a[ii]<a[jj])tmp[k++]=a[ii++]; else tmp[k++]=a[jj++]; } while(ii<=mid)tmp[k++]=a[ii++]; while(jj<=r)tmp[k++]=a[jj++]; for(int i=l;i<=r;i++)a[i]=tmp[i]; } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]); merge_sort(1,n); for(int i=1;i<=n;i++)cout<<a[i]<<" "; return 0; }
洛谷模板:Lucas定理
给定n,m,p(1le n,m,ple 10^51≤n,m,p≤105)
求C(n+m,m)
#include<iostream> #include<cstdio> #include<cstring> #define LL long long using namespace std; int T; LL n,m,p,f[100009]; void pre(){ f[0]=1; for(int i=1;i<=p;i++)f[i]=f[i-1]*i%p; } LL ksm(LL x,LL y){ LL ret=1%p; while(y){ if(y&1)ret=ret*x%p; x=x*x%p; y>>=1; } return ret; } LL C(LL n,LL m){ if(m>n)return 0;//******* return f[n]*ksm(f[m],p-2)%p*ksm(f[n-m],p-2)%p; } LL Lucas(LL n,LL m){ if(!m)return 1; return C(n%p,m%p)*Lucas(n/p,m/p)%p; } int main(){ scanf("%d",&T); while(T--){ scanf("%lld%lld%lld",&n,&m,&p); pre(); printf("%lld ",Lucas(n+m,m)%p); } return 0; }
洛谷模板:ST表
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int f[200002][22]; int n,m,a,b; int query(int l,int r){ int k=log(r-l+1)/log(2); return max(f[l][k],f[r-(1<<k)+1][k]); } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++)scanf("%d",&f[i][0]); for(int i=1;i<=20;i++) for(int j=1;j+(1<<i)-1<=n;j++) f[j][i]=max(f[j][i-1],f[j+(1<<(i-1))][i-1]); for(int i=1;i<=m;i++){ scanf("%d%d",&a,&b); printf("%d ",query(a,b)); } }
洛谷模板:矩阵快速幂
#include<iostream> #include<cstdio> #include<cstring> #define mod 1000000007 using namespace std; int n; struct matrix{ long long m[101][101]; }A; matrix mul(matrix a,matrix b){ matrix t; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ t.m[i][j]=0; for(int k=1;k<=n;k++){ t.m[i][j]=(t.m[i][j]+a.m[i][k]*b.m[k][j])%mod; } } } return t; } matrix fast_mul(matrix a,long long k){ matrix ret=a,now=a; k--; while(k){ if(k&1)ret=mul(ret,now); now=mul(now,now); k>>=1; } return ret; } long long k; int main(){ scanf("%d%lld",&n,&k); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&A.m[i][j]); A=fast_mul(A,k); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++) printf("%lld ",A.m[i][j]); printf(" "); } return 0; }
洛谷模板:乘法逆元
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 3000010 using namespace std; int n,p; int inv[MAXN]; int main(){ scanf("%d%d",&n,&p); inv[1]=1; printf("1 "); for(int i=2;i<=n;i++){ inv[i]=(p-p/i)*1ll*inv[p%i]%p; printf("%d ",inv[i]); } }
#include<iostream> #include<cstdio> #include<cstring> #define LL long long using namespace std; int n,p; LL ksm(LL x,LL y){ int ret=1; while(y){ if(y&1)ret=1LL*ret%p*x%p; x=x*x%p; y>>=1; } return ret%p; } int main(){ scanf("%d%d",&n,&p); for(int i=1;i<=n;i++){ printf("%lld ",ksm(i,p-2)%p); } return 0; }
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,p; void exgcd(int a,int b,int &x,int &y){ if(b==0){ x=1;y=0; return; } exgcd(b,a%b,x,y); int t=x;x=y;y=t-a/b*y; } int main(){ scanf("%d%d",&n,&p); for(register int i=1;i<=n;i++){ int x,y; exgcd(i,p,x,y); printf("%d ",(x+p)%p); } return 0; }
后两个在洛谷T一个点
经典算法:
希望看到有漏下的重要考点能够提出来
感谢