c++ - 将字符串与给定的二维字符数组匹配

标签 c++ arrays data-structures dynamic-memory-allocation

给你一个字符矩阵。该矩阵有 N 行和 M 列。给定一个字符串 s,您必须判断是否可以从给定矩阵生成该字符串。 从矩阵生成字符串的规则是:

您必须从第 1 行中选择字符串的第一个字符,从第 2 行中选择第二个字符,依此类推。 string的第N+1个字符要从第1行中取出,即可以循环遍历各行(第1行在第N行之后)。 如果从一行中选择了一个字符的出现,则不能从该行再次选择相同的出现。 如果可以使用给定规则从矩阵生成给定字符串,则必须打印 Yes,否则打印 No。

输入格式:

第一行由T组成,表示测试用例的数量。 每个测试用例包括: 第一行由两个整数 N 和 M 组成,表示矩阵维度。 接下来的 N 行每行包含 M 个字符。 最后一行由字符串 s 组成。

输出格式: 对于每个测试用例,如果可以生成字符串则打印"is",否则打印“否”。每个测试用例的答案应该换行。

样本输入 1个 3 3 阿巴 xyz bdr 数据库

样本输出 是的

我们从第 1 行选择“a”。现在,我们只能从第 1 行再选择一个“a”,因为已经使用了一个“a”。 同样,“x”来自第 2 行,“b”来自第 3 行。 现在,我们再次回到第一行。 我们从第 1 行选择“a”,从第 2 行选择“y”等等。

#include<iostream>
#include<string>
using namespace std;

int main()
{
int testcase, row, col, x = 0, i = 0;
bool flag = true;
string word;
cin >> testcase; //number of testcases
for (int i = 0; i < testcase; i++)
{
    cin >> row;  //number of rows
    cin >> col;  //number of columns

    char** arr = (char**)malloc(row * sizeof(char *)); //allocating memory for arr pointer to pointer based on the number of rows

    for (int i = 0; i < row; i++)
    {
        arr[i] = (char*)malloc(col * sizeof(char)); //allocating memory for arr pointer 
    }
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            cin >> arr[i][j];
        }
    }
    cin>>word;
    while (x < word.length()) // looping through the given string until it reaches the end of the string
    {
        while (i <= row) // looping through the rows of the 2darray
        {
            for (int j = 0; j < col; j++) //looping through each element in 1d array
            {
                if (i == row) //to ensure that after the last row it goes back again to the first row and starts iterating from the first row
                {
                    i = 0;
                }
                if (word[x] == arr[i][j]) // if character from the string matches the element in the 1st row of 2d array, we will go to the next character of the string and also go to the next row for searching the character in that row.
                {
                    x++;
                    i++;
                }
                else
                {
                    flag = false; // if the value is not found, we will set the flag to false
                }
            }
        }
    }
    if (flag == false)
    {
        cout << "No"<<endl;
    }
    else
    {
        cout << "Yes"<<endl;
    }
}
return 0;

下面的代码没有按预期工作

1
5 8
wxyqkbtk
xpbzexmh
ffkgmqnj
lfyrrwsn
vqfftarq
tswsgdzlpfxithvahmrffgax

最佳答案

为什么你不能只使用 vector 和字符串类而不是所有这些手动 malloc'ing 和逐个字符解析的原因?

#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
    int testcase, row, col;
    cin >> testcase;
    for (int i = 0; i < testcase; i++)
    {
        vector<string> rows;
        string word;
        bool allFound = true;
        int rowIndex = 0;

        cin >> row;
        cin >> col;  // you can ignore this value since we read each row as a string

        // read each row and append to the "rows" vector
        for (int r = 0; r < row; r++)
        {
            string line;
            cin >> line;
            rows.push_back(line);
        }

        // read the test case word    
        cin >> word;

        // for each letter in word, test to see if that same letter
        // exists on the expected row of input
        for (char c : word)
        {
            string& currentRow = rows[rowIndex];
            if (currentRow.find(c) == string::npos)
            {
                allFound = false;
                break;
            }
            rowIndex = (rowIndex + 1) % row;
        }
        cout << (allFound ? "Yes" : "No") << endl;
    }
    return 0;
}

关于c++ - 将字符串与给定的二维字符数组匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55672211/

相关文章:

c++ - 读取 istream 的名称

c++ - 如何在 CUDA 中从稀疏数组表示变为密集数组表示

javascript - 如何在对象中传播嵌套属性?

c++ - C++ Map中如何使用自定义结构

java - java中while循环转换为递归

c++ - 未调用模板化运算符赋值重载

c++ - 使用 alpha 值渲染纹理(贴花)?

python - 如何从二进制字符串中获取字节数组?

Python 列表索引超出范围,即使在分隔索引之后也是如此

javascript - 如何替换对象内部的属性名称? JavaScript