贝伦卡斯泰露

这是我的第一遍dfs篇,题目大意如下:

给我们一个数组a,让我们拆成两个序列,让两个序列相同。

题解:

我们设立两个数组,第一个数组b,第二个数组c,我们先把a数组的第一个数字赋值给a,然后开始dfs,

dfs有四个值,分别为m代表着数组b的当前的位置,s代表着数组c当前的位置,q代表着当前数组a的位置

还有一个n代表着总长度。int dfs(int m, int s, int q, int n)。

(1)如果两个数组的某一个数组长度超过了n/2,则不满足条件;

(2)如果q的大小大于了n说明已经满足条件了;

(3)比较数组a当前位置的值和数组c的长度加一的位置的值在数组b的值,如果相同加入数组c中反之加入数组b中,然后接着dfs

以上就是本题的思路了,接下来附上代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#define ll long long
using namespace std;

const int inf = 0x3f3f3f3f;
const int maxx = 100010;
int a[50];
int b[50];
int c[50];
int dfs(int m, int s, int q, int n);
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i];
        }
        b[1] = a[1];
        if (dfs(1, 0, 2, n) == 1)
        {
            cout << "Frederica Bernkastel" << endl;
        }
        else
        {
            cout << "Furude Rika" << endl;
        }
    }
}
int dfs(int m, int s, int q, int n)
{
    if (m > n / 2 || s > n / 2)
    {
        return 0;
    }
    if (q > n)
    {
        return 1;
    }
    if (a[q] == b[s + 1])
    {
        c[s + 1] = a[q];
        if (dfs(m, s + 1, q + 1, n) == 1) {
            return 1;
        }
    }
    b[m + 1] = a[q];
    return dfs(m + 1, s, q + 1, n);
}
原文地址:https://www.cnblogs.com/csxaxx/p/13360230.html