通过动态编程找到最长的卡片序列

问题描述

我正在尝试编写一个程序,该程序应该使用动态编程来找到经过改组的纸牌中最长的纸牌序列(疯狂的八字样式)的长度。

我们不允许使用标准库(包括向量)。

到目前为止,这是我写的:

程序从文件中读取卡片组,并将其存储在数组中。 然后从crazyUp()函数中,我尝试将数组中的每个元素与上一个元素进行比较,如果它们相等,我们将与应该用作哈希表的dict[]比较进行记忆。

我无法确定maxLength总是返回1的原因,我们将不胜感激。

文件包含卡片的数据,如下所示:

9C
9D
5C
AC
4D
3H
KD
4S

总共52行(52张卡)。

顺序如下:

如果一张牌的等级或西服与前一张相似,则将其添加到该顺序。

将8作为通用卡,可以与任何其他卡匹配,因此得名,疯狂的八!!

示例:

9C
9D
5C
AC
8S

将是有效序列。

但是:

5C
9H
3D

将被完全跳过,因为它们都不像“另一个”。

#include <iostream>
#include <fstream>
#include<sstream>

using namespace std;

int crazyUp(string[]);
int index = 0;

string cards[55];

int dict[55];
int dictIndex = 0;

int main()
{
    string file;
    string word;
    cout << "Please input a file's name..." << endl;
    cin >> file;

    ifstream fileName(file);

    while (fileName >> word) {
        cards[index++] = word;
    }
    
    crazyUp(cards);
    
}


int crazyUp(string cards[]) {
    dict[0] = 0;
    int maxLength = 0;
    int length;

    for (int i = 0; i < index; i++) {
        length = 1;
        for (int j = 0; j < i-1; j++) {
            if (cards[i] == cards[j]) {
                length = max(length,dict[j]+1);
            }
            

        }
        dict[i] = length;
        maxLength = max(maxLength,length);
        cout << maxLength;
    }

    return maxLength;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)