题解:AT_abc344_d [ABC344D] String Bags

· · 题解

这是一篇不用 dp 的题解

题意

给你一个字符串 S,给你 n 个包,每个包里有一些字符串,让你从第 1 个包选择到第 n 个。

对于每一个包你有两种操作

1、什么也不做。

2、选择其中的一个字符串将其接在你已经组成的字符串 T 后面,这个操作消耗 1 人民币。

如何利用最少的人民币将 T 变为 S?

思路

第一眼:暴力搜索,但是超时。

所以需要优化,这是我从我大脑中突然想到了某一个奇奇怪怪的优化!

先初始化出一个数组 have。

have[i][j]:从 S 的第 i 开始可以匹配到第 have[i][j] 个字符。

然后进行广搜。

状态:现在匹配到了字符串的 S 的第 x 个字符,用到了第 y 个包,花费了 z 人民币。

然后就可以快快乐乐的搜索啦!!!

代码

/*
  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[105];
    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][v.sed]=1;
        while(!q.empty()){
            pair<int,PII> k=q.front();q.pop();
            if(k.fst==fl-1){
                cout<<k.sed.sed<<'\n';
                exit(0);
            }
            if(k.sed.fst==n&&k.fst!=fl-1)continue;
            for(PII v:have[k.fst+1]){
                if(v.sed<=k.sed.fst)continue;
                if(vis[v.fst][v.sed])continue;
                if(v.sed==n&&v.fst!=fl-1)break;
                q.push({v.fst,{v.sed,k.sed.sed+1}});
                vis[v.fst][v.sed]=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("abc344d.txt","r",stdin);
//      freopen("","w",stdout);
    int _=1;
    while(_--)my::solve();
    return 0;
}

然后在我的乱搞下成功的在4ms之内跑过了!!!

提交记录