본문 바로가기
개발/코딩

해커랭크(HackerRank) - The Grid Search // C++

by lucidmaj7 2021. 5. 16.
728x90
반응형

해커랭크(HackerRank) - The Grid Search // C++

https://www.hackerrank.com/challenges/the-grid-search/problem

string gridSearch(vector<string> G, vector<string> P) {
    int gridRow = G.size();
    int gridCol = G[0].length();
    int patternRow = P.size();
    int patternCol = P[0].length();

    for(int j = 0 ;j<gridRow-patternRow+1;j++)
    {
        for(int i = 0;i<gridCol-patternCol+1;i++)
        {
            if(
                memcmp((const char*)&(G[j].c_str()[i]),
                (const char*)P[0].c_str(),
                patternCol )==0
            )
            {
                int matchCount = 1;
                for(int k =1 ;k<patternRow;k++)
                {
                    if(
                        memcmp((const char*)&(G[j+k].c_str()[i]),
                        (const char*)P[k].c_str(),
                        patternCol )==0
                    ){
                        matchCount++;
                    }else
                        break;
                }
                if(matchCount ==patternRow )
                {
                    return "YES";
                }
            }       
        }
    }
    return "NO";
}
728x90
반응형

댓글