You are given an n*m grid which contains lower case English letters. How many times does the phrase "saba" appear horizontally, vertically, and diagonally in the grid?
Counting horizontally, vertically and diagonally.
#include<iostream>
#include <vector>
#include <string>
using namespace std;
int TotalCount(vector<string> Str, int ItemCount)
{
string Text = "saba";
string VerticalString = "";
string DiagonalOneString = "";
string DiagonalTwoString = "";
int count = 0;
for (int i = 0; i < ItemCount; ++i)
{
string& currentRow = Str[i];
VerticalString = VerticalString.append(¤tRow.at(0));
DiagonalOneString = DiagonalOneString.append(¤tRow.at(i));
DiagonalTwoString =
DiagonalTwoString.append(¤tRow.at(currentRow.length() - 1 - i));
if ((currentRow.find(Text) != string::npos) || (VerticalString.find(Text) != string::npos) || (DiagonalOneString.find(Text) != string::npos) || (DiagonalTwoString.find(Text) != string::npos))
{
count++;
}
}
return count;
}
int main()
{
int total = 0;
int row;
cin >> row;
vector<string> rows;
// read each row and append to the "rows" vector
for (int r = 0; r < row; r++)
{
string line;
cin >> line;
rows.push_back(line);
}
cout << TotalCount(rows, row);
return 0;
}
Input format
First line: Two integer n and m, where n denotes (1 <= n,m <= 100) the number of rows and m denotes the number of columns in the grid Next n lines: Each line must contain a string of length m which contains lower-case English letters only
Sample Input
5 5
safer
amjad
babol
aaron
songs
Expected Output
2
I am not getting the expected count. Can please someone let me know why the count is coming wrong?
Aucun commentaire:
Enregistrer un commentaire