QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#217661#6366. MessageliqingyangWA 16ms175684kbC++142.3kb2023-10-17 08:26:302023-10-17 08:26:31

Judging History

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

  • [2023-10-17 08:26:31]
  • 评测
  • 测评结果:WA
  • 用时:16ms
  • 内存:175684kb
  • [2023-10-17 08:26:30]
  • 提交

answer

#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
int Task,T,n,m,a[200010],p[26],cnt[26],Cnt[26];
char s[200010],t[200010];
vector<int> S[26];
vector<long long> A[26];
long long ans,V[26];
void dfs(int ps,int pt,long long val)
{
	if(val>=ans)
	{
		return;
	}
	if(pt>m)
	{
		ans=val;
		return;
	}
	int minn=1e9;
	for(int i=0;i<26;i++)
	{
		if(Cnt[i]&&Cnt[i]<cnt[i])
		{
			minn=min(minn,S[i][p[i]]);
		}
	}
	int c=t[pt]-'a';
	if(!(Cnt[c]++))
	{
		int P=upper_bound(S[c].begin(),S[c].end(),ps)-S[c].begin();
		for(int i=P;i+cnt[c]<=S[c].size()&&S[c][i]<minn;i++)
		{
			p[c]=i;
			dfs(S[c][p[c]++],pt+1,val-V[c]+A[c][i]+A[c].back()-A[c][i+cnt[c]]);
		}
		Cnt[c]--,p[c]=0;
		return;
	}
	if(S[c][p[c]]>minn)
	{
		Cnt[c]--;
		return;
	}
	dfs(S[c][p[c]++],pt+1,val);
	Cnt[c]--,p[c]--;
}
long long f[200010][110];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
		cin>>s+1>>t+1;
n=strlen(s+1),m=strlen(t+1);
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		memset(cnt,0,sizeof(cnt));
		for(int i=1;i<=m;i++)
		{
			cnt[t[i]-'a']++;
		}
		if(m<=100)
		{
			memset(f,0x3f,sizeof(f)),f[0][0]=0;
			for(int i=1;i<=n;i++)
			{
				memset(Cnt,0,sizeof(Cnt));
				for(int j=0;j<=m;j++)
				{
					if(j)
					{
						Cnt[t[j]-'a']++;
					}
					long long v=f[i-1][j];
					if(!Cnt[s[i]-'a']||Cnt[s[i]-'a']==cnt[s[i]-'a'])
					{
						f[i][j]=min(f[i][j],v+a[i]);
					}
					if(s[i]==t[j+1])
					{
						f[i][j+1]=min(f[i][j+1],v);
					}
				}
			}
			cout<<(f[n][m]<1e18?f[n][m]:-1)<<endl;
return 0;
		}
		for(int i=0;i<26;i++)
		{
			S[i].clear(),A[i].clear();
			A[i].push_back(0);
		}
		for(int i=1;i<=n;i++)
		{
			S[s[i]-'a'].emplace_back(i);
			A[s[i]-'a'].emplace_back(A[s[i]-'a'].back()+a[i]);
		}
		bool flag=1;
		long long sum=0;
		for(int i=0;i<26;i++)
		{
			if(cnt[i])
			{
				V[i]=1e18;
				if(S[i].size()<cnt[i])
				{
					flag=0;
					break;
				}
				for(int j=0;j+cnt[i]<=S[i].size();j++)
				{
					V[i]=min(V[i],A[i][j]+A[i].back()-A[i][j+cnt[i]]);
				}
				sum+=V[i];
			}
		}
		if(!flag)
		{
			cout<<-1<<endl;
return 0;
		}
		ans=1e18,dfs(0,1,sum);
		for(int i=0;i<26;i++)
		{
			if(!cnt[i])
			{
				ans+=A[i].back();
			}
		}
		cout<<(ans<1e18?ans:-1)<<endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 16ms
memory: 175684kb

input:

ababccb
abc
7 2 2 4 3 2 1

output:

7

result:

ok single line: '7'

Test #2:

score: -100
Wrong Answer
time: 4ms
memory: 175548kb

input:

babab
baab
2 1 3 2 4

output:

-1

result:

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