zoj3494——BCD_code【ac机】【数位dp】

ac机  套   数位dp


写的时候错误不断  手残不断 orz


读入拒绝高精度。。。

大不了特判

打死不写高精度。。。



#include <queue>
#include <cstdio>
#define MOD 1000000009
using namespace std;

int len,n,T,con;
char s[25],c;
int u,v;
int num[805];

struct node {
	bool word;
	node *ch[2],*fail;
	int ret,id;
}tr[200010],*root,Fail;
int cnt = -1;

queue<node*>Q;

node *NEW() {
	tr[++cnt].id = cnt;
	return &tr[cnt];
}

void read_str () {
	len = -1;
	c = getchar();
	for(;c > '1' || c < '0'; c = getchar());
	for(;c <= '1' && c >= '0';c = getchar())
		s[++len] = c;
}

int read_int () {
	char c = getchar();
	int re = 0;
	for(;c > '9' || c < '0';c = getchar());
	for(;c <= '9' && c >= '0';c = getchar())
		re = re * 10 + c - '0';
	return re;
}

void build_tr () {
	int ID;
	node *p = root;
	for(int i = 0;i <= len;++i) {
		ID = s[i] - '0';
		if(!p->ch[ID])
			p->ch[ID] = NEW();
		p = p->ch[ID];
	}
	p->word = 1;
}

void build_ac () {
	Q.push(root);
	node *p;
	while(!Q.empty()) {
		p = Q.front();
		Q.pop();
		for(int i = 0;i <= 1;++i) {
			if(p->ch[i]) {
				if(p == root)
					p->ch[i]->fail = root;
				else {
					p->ch[i]->fail = p->fail->ch[i];
					if(p->ch[i]->fail->word)
						p->ch[i]->word = 1;
				}
				Q.push(p->ch[i]);
			}
			else {
				if(p == root)
					p->ch[i] = root;
				else p->ch[i] = p->fail->ch[i];
			}
		}
	}
}

int dp[805][2000];
int hash[805][2000];

node* change (node *now,int Num) {
	int t = 4;
	for(int i = 1;i <= 4;++i) {
		if(Num >= (1 << --t)) {
			Num -= 1 << t;
			now = now->ch[1];
		}
		else now = now->ch[0];
		if(now->word)
				return &Fail;
	}
	return now;
}

int DP (int pos,bool limit,node *now,bool val) {
	if(pos == num[0] + 1)
		return 1;
	if(hash[pos][now->id] == con && !limit  && !val)
		return dp[pos][now->id];
	int re = 0;
	node *temp;
	int upp = limit ? num[pos] : 9;
	for(int i = 0;i <= upp;++i) {
		if(i == 0 && val) {
			if(pos != num[0]) {
				re += (DP(pos + 1,0,now,1) % MOD);
				re %= MOD;
			}
			else {
				re += (DP(pos + 1,0,change(now,0),1) % MOD);
				re %= MOD;
			}
			continue;
		}
		temp = change(now,i);
		if(temp->ret != 1) {
			re += (DP(pos + 1,limit && i == upp,temp,0) % MOD);
			re %= MOD;
		}
	}
	if(!limit && hash[pos][now->id] != con && !val) {
		hash[pos][now->id] = con;
		dp[pos][now->id] = re;
	}
	return re;
}

void get_bign (bool f) {
	char c = getchar();
	num[0] = 0;
	for(;c > '9' || c < '0';c = getchar());
	for(;c <= '9' && c >= '0';c = getchar()) {
		num[++num[0]] = c - '0';
	}
	if(f) {
		int te = num[0];
		--num[te];
		while(num[te] == -1) {
			num[te] += 10;
			num[te - 1]--;
			te--;
		}
	}
}

int main () {
	Fail.ret = 1;
	T = read_int();
	while(T--) {
		root = NEW();
		n = read_int();
		while(n--) {
			read_str();
			build_tr();
		}
		build_ac();
		++con;
		get_bign(1);
		u = DP(1,1,root,1);
		++con;
		get_bign(0);
		v = DP(1,1,root,1);
		printf("%d\n",(v - u + MOD) % MOD);
	}
	return 0;
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是ZOJ1626的C++ AC代码,使用了旋转卡壳算法: ```c++ #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #define MAXN 100010 #define eps 1e-8 #define INF 1e20 using namespace std; struct point { double x,y; friend point operator -(point a,point b) { point res; res.x=a.x-b.x; res.y=a.y-b.y; return res; } friend bool operator <(point a,point b) { if(fabs(a.x-b.x)<eps) return a.y<b.y; return a.x<b.x; } friend double operator *(point a,point b) { return a.x*b.y-a.y*b.x; } friend double dis(point a,point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } }a[MAXN],b[MAXN],st[MAXN]; int n; double ans=INF; int cmp(point a,point b) { double tmp=(a-b)*(a[1]-b); if(fabs(tmp)<eps) return dis(a,a[1])-dis(b,a[1])<0; return tmp>0; } int main() { while(~scanf("%d",&n) && n) { for(int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y); sort(a+1,a+n+1); int tot=0; for(int i=1;i<=n;i++) { while(tot>=2 && (st[tot]-st[tot-1])*(a[i]-st[tot])<0) tot--; st[++tot]=a[i]; } int k=tot; for(int i=n-1;i>=1;i--) { while(tot>k && (st[tot]-st[tot-1])*(a[i]-st[tot])<0) tot--; st[++tot]=a[i]; } tot--; for(int i=1;i<=tot;i++) b[i]=st[i]; int tmp=1; for(int i=2;i<=tot;i++) if(b[i].y<b[tmp].y) tmp=i; swap(b[1],b[tmp]); sort(b+2,b+tot+1,cmp); st[1]=b[1]; st[2]=b[2]; k=2; for(int i=3;i<=tot;i++) { while(k>1 && (st[k]-st[k-1])*(b[i]-st[k])<=0) k--; st[++k]=b[i]; } double ans=0; if(k==2) ans=dis(st[1],st[2]); else { st[k+1]=st[1]; for(int i=1;i<=k;i++) for(int j=1;j<=k;j++) ans=max(ans,dis(st[i],st[j])); } printf("%.2lf\n",ans/2); } return 0; } ``` 其中,结构体 `point` 表示二维平面上的一个点,包含了点的坐标和一些基本操作。函数 `cmp` 是旋转卡壳算法中的比较函数,按照点到起点的极角从小到大排序。在主函数中,先使用 Graham 扫描法求出点集的凸包,然后按照旋转卡壳的步骤,求出凸包上的最远点对距离作为最小直径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值