QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#526031#6366. Messagechenxinyang2006WA 166ms234228kbC++203.9kb2024-08-21 09:57:142024-08-21 09:57:14

Judging History

你现在查看的是最新测评结果

  • [2024-08-21 09:57:14]
  • 评测
  • 测评结果:WA
  • 用时:166ms
  • 内存:234228kb
  • [2024-08-21 09:57:14]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,j,k) for(int i=(j);i<=(k);i++)
#define per(i,j,k) for(int i=(j);i>=(k);i--)
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define db double
#define ldb long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define eb emplace_back
#define SZ(S) (int)S.size()
//#define mod 998244353
//#define mod 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3f
using namespace std;

template <class T>
void chkmax(T &x,T y){
	if(x < y) x = y;
}

template <class T>
void chkmin(T &x,T y){
	if(x > y) x = y;
}

inline int popcnt(int x){
	return __builtin_popcount(x);
}

inline int ctz(int x){
	return __builtin_ctz(x);
}


/*ll power(ll p,int k = mod - 2){
	ll ans = 1;
	while(k){
		if(k % 2 == 1) ans = ans * p % mod;
		p = p * p % mod;
		k /= 2;	
	}
	return ans;
}*/
int n,m;
char _s[200005],_t[200005];
int w[200005];
int occ[2][26],delta[200005];

int sz,k,top,req,_len[60],_bas[60],_occ[60];
char s[60][200005],t[200005];
int nxt[200005],idx[200005],res[200005],fail[200005];//s 匹配 t 的哪些区间
ll cst[60][200005];
int ch[60][200005];
ll _sum[200005];
void slv(int p){
/*	printf("slv p=%d\n",p);
	rep(i,1,_len[p]) printf("%c",s[p][i]);
	printf("\n");
	rep(i,1,k) printf("%c",t[i]);
	printf("\n");*/
	int j = 0;
	rep(i,2,_len[p]){
		while(j && s[p][j + 1] != s[p][i]) j = fail[j];
		if(s[p][j + 1] == s[p][i]) j++;
		fail[i] = j;
	}
	j = 0;
	rep(i,1,k){
		while(j && s[p][j + 1] != t[i]) j = fail[j];
		if(s[p][j + 1] == t[i]) j++;
		res[i] = 0;
		if(j == _len[p]){
			res[i] = 1;
			j = fail[j];
		}
	}
/*	rep(i,1,k) printf("%d",res[i]);
	printf("\n");*/
	top = req = 0;
	rep(i,1,n) if(_s[i] == _occ[p]) _sum[++top] = w[i];
	rep(i,1,top) _sum[i] += _sum[i - 1];
//	rep(i,1,top) printf("%lld ",_sum[i]);
//	printf("\n");
	rep(i,1,m) if(_t[i] == _occ[p]) req++;
//	printf("req=%d\n",req);
	rep(i,1,n){
		if(nxt[i] > k || !res[nxt[i]]){
			cst[p][i] = linf;
			continue;
		}
		cst[p][i] = 0;
		ch[p][i] = idx[nxt[i] + _len[p] - 1];
//		printf("%d->%d ",i,ch[p][i]);
		if(!_occ[p]) continue;
		if(nxt[i] + req - 1 > top) cst[p][i] = linf;
		else cst[p][i] = _sum[nxt[i] - 1] + _sum[top] - _sum[nxt[i] + req - 1];
	}
//	printf("\n");
/*	rep(i,1,n) printf("%d ",nxt[i]);
	printf("\n");
	rep(i,1,n) printf("(%d,%lld) ",ch[p][i],cst[p][i]);
	printf("\n");*/
}

ll dp[60][200005];
int main(){	
//	freopen("test.in","r",stdin);
	scanf("%s",_s + 1);
	scanf("%s",_t + 1);
	n = strlen(_s + 1);m = strlen(_t + 1);
	rep(c,0,25){
		occ[0][c] = inf;
		occ[1][c] = 0;
	}
	rep(i,1,m){
		chkmin(occ[0][_t[i] - 'a'],i);
		chkmax(occ[1][_t[i] - 'a'],i);
	}
	rep(c,0,25){
		if(occ[0][c] > occ[1][c]) continue;
		delta[occ[0][c]] += 1 << c;
		delta[occ[1][c] + 1] -= 1 << c;
	}
	rep(i,1,m) delta[i] += delta[i - 1];
	for(int l = 1,r;l <= m;l = r + 1){
		r = l;
		while(r < m && delta[r] == delta[r + 1]) r++;
		_bas[++sz] = delta[l];
		if(occ[0][_t[l] - 'a'] == l) _occ[sz] = _t[l];
		rep(i,l,r) s[sz][++_len[sz]] = _t[i];
	}
/*	rep(i,1,sz){
		printf("seg %d bas=%d occ=%d\n",i,_bas[i],_occ[i]);
		rep(j,1,_len[i]) printf("%c",s[i][j]);
		printf("\n");
	}*/
	rep(i,1,n) scanf("%d",&w[i]);
	rep(p,1,sz){
		k = 0;
		rep(i,1,n){
			nxt[i] = 0;
			if((_bas[p] >> (_s[i] - 'a')) & 1){
				nxt[i] = ++k;
				idx[k] = i;
				t[k] = _s[i];
			}
		}
		nxt[n + 1] = n + 1;
		per(i,n,1) if(!nxt[i]) nxt[i] = nxt[i + 1];
		slv(p);
	}

	ll answer = linf;
	memset(dp,0x3f,sizeof(dp));
	dp[0][0] = 0;
	rep(i,1,n){
		rep(p,1,sz){
			if(_s[i] == _occ[p]) chkmin(dp[p - 1][i],dp[p - 1][i - 1]);
			if(cst[p][i] == linf) continue;
			chkmin(dp[p][ch[p][i]],dp[p - 1][i - 1] + cst[p][i]);
		}
		chkmin(answer,dp[sz][i]);
	}	
	if(answer < linf) printf("%lld\n",answer);
	else printf("You better start from scratch man...\n");
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 115532kb

input:

ababccb
abc
7 2 2 4 3 2 1

output:

7

result:

ok single line: '7'

Test #2:

score: 0
Accepted
time: 0ms
memory: 117820kb

input:

babab
baab
2 1 3 2 4

output:

You better start from scratch man...

result:

ok single line: 'You better start from scratch man...'

Test #3:

score: 0
Accepted
time: 32ms
memory: 119524kb

input:

bbaaaabbbbbabaababbaaabbbabaaaababaababaabababbbbabbbbababaabaaabbabaaaabbbabbaababababbabbbabbaababaaaaaabbabbbababbaabbbabbbabbbabaababaaaaaabaaababbbbbbaabaabaaaaaaabbbaaabaabbbababbbbbabbababaababaabbababbaababbbbbbbbbbbaabbbbbabaaabaabaaabaaaaabaaaabbbbbbbaaaabaabbbababbaaaabbabababbbabbbbabbab...

output:

You better start from scratch man...

result:

ok single line: 'You better start from scratch man...'

Test #4:

score: 0
Accepted
time: 47ms
memory: 140000kb

input:

bdabcfbdfcffebebcabbadacbbaeeaffbdedeedfabefdfdbddcecdaaddafdfbbdceccedebcecdfbcfbaafcefeecffdabfaacbeeecfeffaaafaefdcdaaeaeecfafcdadbfbccbdecacfeabdbfcacafebdcfbfbebacbffaecbfbcedccabbdecabaebbbdbcfbaeadfcadfadfaebaddbebfcbefdabdcefbbdaaaabcefedabcabcafedcfadedfdcbbccbffdcfdfcfcdfcfbbdabdbbeecafecc...

output:

You better start from scratch man...

result:

ok single line: 'You better start from scratch man...'

Test #5:

score: 0
Accepted
time: 166ms
memory: 234228kb

input:

soibsuydrizsuvymezuyrewmgwpnzxgyggpzjkdzooisgzbkfqjzkfcklluotqpwganvksoqtzixkfkrtqobdnregwgkxjwzsruvhztscxjyqlhfytomzhxiglxemdhkjnskrsqngojffogrkbygmdgzfwrlhwhhngqpjpepqgynsdybhpuaqhgjroijqofiwnxgxdmhofwsjnmwitruiesefzmabcfsyzrrruidewjowfkwwwqhztsmmtdnejlqhkmbpmknlxijnmzbtqviburbqwufipqsrqplcelovsxz...

output:

You better start from scratch man...

result:

ok single line: 'You better start from scratch man...'

Test #6:

score: -100
Wrong Answer
time: 24ms
memory: 117428kb

input:

bbaaaabbbbbabaababbaaabbbabaaaababaababaabababbbbabbbbababaabaaabbabaaaabbbabbaababababbabbbabbaababaaaaaabbabbbababbaabbbabbbabbbabaababaaaaaabaaababbbbbbaabaabaaaaaaabbbaaabaabbbababbbbbabbababaababaabbababbaababbbbbbbbbbbaabbbbbabaaabaabaaabaaaaabaaaabbbbbbbaaaabaabbbababbaaaabbabababbbabbbbabbab...

output:

You better start from scratch man...

result:

wrong answer 1st lines differ - expected: '0', found: 'You better start from scratch man...'