Wannafly挑战赛28 B.msc和mcc(字符串、思维、预处理)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wwwlps/article/details/83958373

题目:https://ac.nowcoder.com/acm/contest/217/B

一段区间存在两个不重叠的序列,问有多少个区间

题解:先写出这两个序列的8中排列情况,然后预处理字符串,用nextz[x][y]表示下标为x的字符后面第一次出现y的位置,然后暴力枚举每一个x(1到n),找到符合条件(8中排列之一)的相对最短的y,最后累加ans+=n-posy+1;

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include <vector>
#include <cmath>
#include<queue>
#include <stack>
#include <map>
#define maxn 100005
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
string s;
int n,nextz[maxn][30];
char tstr[8][7]={"mscmcc","mmsccc","mmccsc","mmcscc","mccmsc","mcmcsc","msmccc","mcmscc"};
void init()
{
	for(int i='a';i<='z';i++)
	{
		for(int j=n;j>=1;j--)
		{
			nextz[j-1][i-'a']=nextz[j][i-'a'];
			if(s[j]==char(i))
				nextz[j-1][i-'a']=j;
		}
	}
}
void solve()
{
	LL now,ans;
	ans=0;
	for(int i=1;i<=n;i++)
	{
		LL zv=n+1;
		for(int u=0;u<8;u++)
		{
			now=i-1;
			for(int v=0;v<6;v++)
			{
				now=nextz[now][tstr[u][v]-'a'];
				if(now==0)
					break;
			}
			if(now!=0)
				zv=min(zv,now);	
		}
		ans+=n-zv+1;
	}
	cout << ans << endl;
}
int main()
{
	cin>>n;
	cin>>s;
	s=" "+s;
	init();
	solve();
	return 0;	
} 

猜你喜欢

转载自blog.csdn.net/wwwlps/article/details/83958373