将字符串与给定的2d char数组匹配

时间:2019-04-14 05:25:25

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

您将获得一个字符矩阵。矩阵有N行和M列。给定字符串s,您必须确定是否有可能从给定的矩阵生成该字符串。 从矩阵生成字符串的规则是:

您必须从第1行中选择字符串的第一个字符,然后从第2行中选择第二个字符,依此类推。字符串的第N + 1个字符将从第1行中选取,也就是说,您可以循环方式遍历各行(第1行在N行之后)。 如果从某行中选取一个字符出现,则不能从该行中再次选取相同的出现。 如果可以使用给定的规则从矩阵生成给定的字符串,则必须打印“是”,否则请打印“否”。

输入格式:

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

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

样品输入 1个 3 3 aba y bdr axbaydb

样品输出 是的

我们从第1行中选择“ a”。现在,由于已经使用了一个“ a”,因此我们只能从第1行中再选择一个“ a”。 同样,第2行中的“ x”,第3行中的“ b”。 现在,我们再次回到第一行。 我们从第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

1 个答案:

答案 0 :(得分:1)

您为什么不能仅仅使用vector和string类来代替所有本本手册对char进行手动分配和解析的原因?

#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;
}