L3-2 还原文件【dfs】

L3-2 还原文件【dfs】

在这里插入图片描述
在这里插入图片描述

这个题,感觉太傻*了,用字符串模拟就是调不出来,一直std报错,服了。

#include<bits/stdc++.h>
using namespace std;

#define int long long

int n,m;
vector<int> vs;
vector<int> vt;
vector<int> v[105];
vector<int> ans;


bool note[105];

void dfs(int now){
    
    
    if(ans.size() == m){
    
    
        vt = ans;
        return;
    }
    int i,j;
    for(i = 1;i <= m;++i){
    
    
        if(!note[i]){
    
    
            for(j = 0;j < v[i].size();++j){
    
    
                if(v[i][j] != vs[now + j])
                    break;
            }
            if(j == v[i].size()){
    
    
                ans.push_back(i);
                note[i] = true;
                dfs(now + j - 1);
                note[i] = false;
                ans.pop_back();
            }
        }
    }
}

signed main(){
    
    
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n;
    int p,q;
    for(int i = 1;i <= n;++i){
    
    
        cin>>p;
        vs.push_back(p);
    }
    cin>>m;
    for(int i = 1;i <= m;++i){
    
    
        cin>>p;
        for(int j = 1;j <= p;++j){
    
    
            cin>>q;
            v[i].push_back(q);
        }
    }
    dfs(0);
    cout<<vt[0];
    for(int i = 1;i < vt.size();++i)
        cout<<" "<<vt[i];
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45985728/article/details/123752880