昵称检索

题目链接

  • 本质不同的合法日期至多只有366个,我们可以先生成所有合法日期,再采用和维护名字一样的方法统计
点击查看代码
#include <bits/stdc++.h>
#define pii pair<int,int>
using namespace std;
int f[1000005],cnt[1000005];
string na[100005];
string e[31]={"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
string date[505];
const int y[]={31,29,31,30,31,30,31,31,30,31,30,31};
unordered_map<int,int>ys;
queue<pii>q[30];
int main()
{
	ys[-1]=0;
	for(int i=0;i<12;i++)
	{
		ys[i]=ys[i-1]+y[i];
	}
	for(int i=0;i<12;i++)
	{
		for(int j=0;j<y[i];j++)
		{
			date[ys[i-1]+j+1]=e[i]+e[j];
			reverse(date[ys[i-1]+j+1].begin(),date[ys[i-1]+j+1].end());
		}
	}
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		int n,m;
		cin>>n>>m;
		string s;
		cin>>s;
		for(int i=0;i<26;i++)
		{
			while(!q[i].empty())
			{
				q[i].pop();
			}
		}
		for(int i=1;i<=n;i++)
		{
			cin>>na[i];
			q[na[i][0]-'a'].push(make_pair(i,0));
		}
		for(int i=0;i<m;i++)
		{
			cnt[i]=0;
			if(s[i]>='a'&&s[i]<='z')
			{
				int l=q[s[i]-'a'].size();
				while(l--)
				{
					pii n1=q[s[i]-'a'].front();
					q[s[i]-'a'].pop();
					if(n1.second==na[n1.first].size()-1)
					{
						f[i]++;
					}
					else
					{
						q[na[n1.first][n1.second+1]-'a'].push(make_pair(n1.first,n1.second+1));
					}
				}
			}
		}
		for(int i=0;i<10;i++)
		{
			while(!q[i].empty())
			{
				q[i].pop();
			}
		}
		for(int i=1;i<=366;i++)
		{
			q[date[i][0]-'0'].push(make_pair(i,0));
		}
		for(int i=m-1;i>=0;i--)
		{
			if(s[i]>='0'&&s[i]<='9')
			{
				int l=q[s[i]-'0'].size();
				while(l--)
				{
					pii n1=q[s[i]-'0'].front();
					q[s[i]-'0'].pop();
					if(n1.second==3)
					{
						cnt[i]++;
					}
					else
					{
						q[date[n1.first][n1.second+1]-'0'].push(make_pair(n1.first,n1.second+1));
					}
				}
			}
		}
		int sum=0,ans=0;
		for(int i=m-1;i>=0;i--)
		{
			sum+=cnt[i];
			ans=ans+f[i]*sum;
			f[i]=0;
		}
		cout<<ans<<endl;
	}
	return 0;
}
posted @ 2024-07-31 10:46  D06  阅读(13)  评论(0编辑  收藏  举报