求调 D
  • 板块学术版
  • 楼主coderJerry
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/3/9 21:44
  • 上次更新2024/3/9 23:24:09
查看原帖
求调 D
953589
coderJerry楼主2024/3/9 21:44

跳的我脑子都要炸了

#include <bits/stdc++.h>
using namespace std;
int n,a,dp[1000010];
string t,s[1000010];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    for(int i=1;i<=1000000;i++) dp[i]=-1;
    dp[0]=0;
    cin>>t>>n;
    int len=t.size();
    for(int i=1;i<=n;i++){
        cin>>a;
        for(int h=1;h<=a;h++) cin>>s[h];
        for(int j=len-1;j>=0;j--){
          for(int u=1;u<=a;u++){
            for(int k=0;k<s[u].size();k++){
                if (j-k>=0&&t.substr(j-k,k)==s[i]&&dp[j-k]!=-1){
                    int tmp=dp[j-k]+1;
                    if(dp[j]==-1||dp[j]>tmp) dp[j]=tmp;
                } 
            }
          }
        }
    }
    cout<<dp[len]<<endl;
    return 0;
}
2024/3/9 21:44
加载中...