大江东去,浪淘尽,千古风流人物。故垒西边,人道是,三国周郎赤壁。乱石穿空,惊涛拍岸,卷起千堆雪。江山如画,一时多少豪杰。遥想公瑾当年,小乔初嫁了,雄姿英发。羽扇纶巾,谈笑间,樯橹灰飞烟灭。故国神游,多情应笑我,早生华发。人生如梦,一尊还酹江月。

Comet OJ 【noip模拟Day2】T2

一道类似康拓展开的数学题,结合了字符串的考点

这是题解的下载链接click here

难点在于如何判断字符串是由哪些单词组成的,正解是字典树,也可以hash,用map能拿90分,手写hash表可以AC

这里给出map代码和手写hash代码

STL map

#include<bits/stdc++.h>
using namespace std;

#define go(i,a,b) for(int i=a;i<=b;++i)
#define com(i,a,b) for(int i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define int long long
#define lowbit(x) (x&-x)
#pragma GCC	optimize(2)

const int inf=0x3f3f3f3f,mod=1e9+7,N=1e6+10;

int n,m,len[N],jc[N],inv[N],c[N];
bool vis[N];
string s[N],b;
map<string,int>id;

inline int mul(const int &x,const int &y){
	return x*y>=mod?x*y%mod:x*y;
}
inline int add(const int &x,const int &y){
	return x+y>=mod?x+y-mod:x+y;
}

inline void init(){
	cin>>n>>m;
	jc[0]=1;
	go(i,1,n) jc[i]=mul(jc[i-1],i);
	inv[0]=inv[1]=1;
	go(i,2,n) inv[i]=mul((mod-mod/i),inv[mod%i]);
	go(i,2,n) inv[i]=mul(inv[i],inv[i-1]);
	go(i,1,n) cin>>s[i];
	sort(s+1,s+n+1);
	go(i,1,n){
		id[s[i]]=i;//标号必须在排序之后标号,否则会全WA 
		len[i]=s[i].length();
	}
	cin>>b;
}

inline int C(int n,int m){
	return jc[n]*inv[m]%mod*inv[n-m]%mod;
}
inline void add(int x){
	for(;x<=n;x+=lowbit(x)) ++c[x];
}
inline int ask(int x){
	int ans=0;
	for(;x;x-=lowbit(x)) ans+=c[x];
	return ans;
}

signed main(){
	init();
	int ans=0;
	string c;
	int i=0;
	for(int pos=0;pos<b.length();++pos){
		int cnt=0;
		c+=b[pos];
		if(id.count(c)){
			++i;
			int j=id[c];
			string tmp;
			swap(c,tmp);
			add(j);
			cnt=j-ask(j-1)-1;
			ans=add(ans,mul(mul(cnt,C(n-i,m-i)),jc[m-i]));
		}
	}
	printf("%lld",(ans+1)%mod);
	return 0;
}

手写hash

#include<bits/stdc++.h>
using namespace std;

#define go(i,a,b) for(int i=a;i<=b;++i)
#define com(i,a,b) for(int i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x&-x)
#define int long long

const int inf=0x3f3f3f3f,mod=1e9+7,N=1e6+10,P=1e7+13;
typedef long long ll;

int n,m,len[N],jc[N],inv[N],c[N],has[N],head[N*10],cn=0;
string s[N],b;
struct edge{
	int nxt,p;
}e[N];
void insert(int x,int p){
	e[cn]=(edge){head[x],p};
	head[x]=cn++;
}
int query(int x,int pos){
	for(int i=head[x];i+1;i=e[i].nxt){
		int p=e[i].p;
		bool ok=1;
		for(int j=len[p]-1,k=pos;j>=0;--j,--k){
			if(s[p][j]!=b[k]){
				ok=0;
				break;
			}
		}
		if(ok) return p;
	}
	return 0;
}

inline int add(const int &x,const int &y){
	return x+y>=mod?x+y-mod:x+y;
}
inline int mul(const int &x,const int &y){
	return 1ll*x*y>=mod?1ll*x*y%mod:x*y;
}

inline void init(){
	cin>>n>>m;
	jc[0]=1;
	go(i,1,n) jc[i]=mul(jc[i-1],i);
	inv[0]=inv[1]=1;
	go(i,2,n) inv[i]=mul((mod-mod/i),inv[mod%i]);
	go(i,2,n) inv[i]=mul(inv[i],inv[i-1]);
	go(i,1,n) cin>>s[i];
	sort(s+1,s+n+1);
	go(i,1,n){
		len[i]=s[i].length();
		for(int j=0;j<len[i];++j){
			has[i]=((ll)has[i]*131+(ll)(s[i][j]))%P;
		}
		insert(has[i],i);
	}
	cin>>b;
}

inline int C(int n,int m){
	return mul(mul(jc[n],inv[m]),inv[n-m]);
}
inline void add(int x){
	for(;x<=n;x+=lowbit(x)) ++c[x];
}
inline int ask(int x){
	int ans=0;
	for(;x;x-=lowbit(x)) ans+=c[x];
	return ans;
}

signed main(){
	//freopen("input.txt","r",stdin);
	mem(head,-1);
	init();
	int c=0;
	int ans=0,i=0,j,l=b.length(),cnt;
	for(int pos=0;pos<l;++pos){
		cnt=0;
		c=((ll)c*131+(ll)b[pos])%P;
		if((j=query(c,pos))){
			++i;
			c=0;
			add(j);
			cnt=j-ask(j-1)-1;
			ans=add(ans,mul(mul(cnt,C(n-i,m-i)),jc[m-i]));
		}
	}
	printf("%lld",(ans+1)%mod);
	return 0;
}

用字典树更快,这里给出一位同学的代码

#include<bits/stdc++.h>
using namespace std;

typedef unsigned long long ll;
const int maxn=1e6+5;
const int mod=1e9+7;
int n,k,cnt;
int siz[maxn*26],ch[maxn][30],fac[maxn];
int ok[maxn*26];
char s[maxn],c[maxn];

void ins(char s[]){
	ll len=strlen(s+1),rt=1;
	for(ll i=1;i<=len;i++){
		ll x=s[i]-'a'+1;
		++siz[rt];
		if(!ch[rt][x]) ch[rt][x]=++cnt;
		rt=ch[rt][x];
	}
	ok[rt]=1,++siz[rt];
}

void solve(){
	ll len=strlen(c+1),rt=1,ans=0;
	for(ll i=1;i<=len;i++){
		ll x=c[i]-'a'+1,ord=0;
		--siz[rt];
		for(ll j=1;j<x;j++) ord=(ord+siz[ch[rt][j]])%mod;
		ans=(ans+ord*fac[k]%mod)%mod;
		rt=ch[rt][x];
		if(ok[rt]){
			--k;
			--siz[rt];
			rt=1;
		}
	}
	printf("%d",(ans+1)%mod);
}

int main(){
	//freopen("input.txt","r",stdin);
	scanf("%d%d",&n,&k);
	fac[1]=cnt=1;
	for(ll i=2,j=n-k+1;j<=n-1;i++,j++){
		fac[i]=1ll*fac[i-1]*j%mod;
	}
	for(ll i=1;i<=n;i++){
		scanf("%s",s+1);
		ins(s);
	}
	scanf("%s",c+1);
	solve();
	return 0;
}
posted @ 2019-09-29 11:36  White_star  阅读(152)  评论(0编辑  收藏  举报
}