abc D求调
  • 板块学术版
  • 楼主kevinZ99
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/3/9 23:10
  • 上次更新2024/3/10 09:50:44
查看原帖
abc D求调
1117080
kevinZ99楼主2024/3/9 23:10
/*
  Author:Kevin Z K Y
  t_limit:
  m_limit:
*/
//mod 998244353 \ 1e9+7 \ 1e9+9 \ 19260817 \ 104857601
#include <bits/stdc++.h>
#define putendl putchar('\n')
#define putspace putchar(' ')
#define rep(f,a) for(auto (f):(a))
#define up(a,b,c) for(int (a)=(b);(a)<=(c);(a)++)
#define dn(a,b,c) for(int (a)=(b);(a)>=(c);(a)--)
using namespace std;
using vi = vector<int> ;
using ll = long long ;
using hint = __int128 ;
using PII = pair<int,int> ;
using PLL = pair<ll,ll> ;
const double eps=1e-6;
namespace IO{
	#define gc() getchar()
    ll max(ll x,ll y){return x>=y?x:y;}
    ll min(ll x,ll y){return x<=y?x:y;}
    ll abs(ll x){return x<=0?-x:x;}
	double fabs(double x){return x<-eps?-x:x;}
	hint readh(){char ch;hint x=0,y=1;while(ch=gc(),(ch<48||ch>57)&&ch!=-1)if(ch==45)y=-1;x=ch-48;while(ch=gc(),47<ch&&ch<58)x=x*10+(ch-48);return x*y;}
	ll readl(){char ch;ll x=0,y=1;while(ch=gc(),(ch<48||ch>57)&&ch!=-1)if(ch==45)y=-1;x=ch-48;while(ch=gc(),47<ch&&ch<58)x=x*10+(ch-48);return x*y;}
	int readi(){char ch;int x=0,y=1;while(ch=gc(),(ch<48||ch>57)&&ch!=-1)if(ch==45)y=-1;x=ch-48;while(ch=gc(),47<ch&&ch<58)x=x*10+(ch-48);return x*y;}
	template<typename T>
	void writei(T x){if(x<0)putchar('-'),x=-x;int index_output=0;char s[40];do{s[index_output++]=x%10+48,x/=10;}while(x);while(index_output)putchar(s[--index_output]);}
	template<typename T>
	void writeis(T x,char c){writei(x);putchar(c);}
	void writess(string &s,int start){int index_output=start;while(s[index_output]!='\0')putchar(s[index_output++]);}
	void writesse(string &s,int start,int end){int index_output=start;while(s[index_output]!='\0'&&index_output<end+1)putchar(s[index_output++]);}
}
using namespace IO;
namespace my{
	void IOS(){ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);}
	string ot;
	string f;
	int n;
	int fl;
	bitset<105> vis;
	vector<string>t[105];
	int len[105];
	vector<PII>have[105];
	bool end=false;
	void init(){
		up(i,1,n){
			up(j,0,len[i]-1){
				int tl=t[i][j].length();
				up(k,0,fl-tl+1){
					bool flag=false;
					up(l,0,tl-1)
						if(f[k+l]!=t[i][j][l]){
							flag=true;
							break;
						}
					if(!flag)have[k].push_back({k+tl-1,i});
					if(k+tl-1==fl-1)end=true;
				}
			}
		}
		if(!end){
			cout<<"-1"<<'\n';
			exit(0);
		}
	}
	void bfs(){
		#define fst first
		#define sed second
		queue<pair<int,PII>>q;
		for(PII v:have[0])q.push({v.fst,{v.sed,1}}),vis[v.fst]=true;
		while(!q.empty()){
			pair<int,PII> k=q.front();q.pop();
			if(k.fst==fl-1){
				cout<<k.sed.sed<<'\n';
				exit(0);
			}
			for(PII v:have[k.fst+1]){
				if(v.sed<=k.sed.fst)continue;
				if(vis[v.fst])continue;
				q.push({v.fst,{v.sed,k.sed.sed+1}});
				vis[v.fst]=true;
			}
		}
		#undef fst
		#undef sed
	}
	void solve(){
		IOS();
		cin>>f;
		fl=f.length();
		cin>>n;
		up(i,1,n){
			cin>>len[i];
			up(j,1,len[i]){
				string s;
				cin>>s;
				t[i].push_back(s);
			}
		}
		init();
		bfs();
		cout<<"-1"<<'\n';
	}
}
int main(){
//  freopen("","r",stdin);
//  freopen("","w",stdout);
	int _=1;
	while(_--)my::solve();
	return 0;
}

思路:

找到从i开始可以向后匹配到的位置。

然后利用广搜进行搜索。

但是Wa*5

2024/3/9 23:10
加载中...