匹配文本文件中的模式

时间:2019-03-28 12:49:10

标签: c++

我有一个文本文件,其中的某个模式需要在另一个文件中识别

图案形状如下:

| |
###O
| |

风景就是这样:

   | |                                
    ###O                               
    | |           | |                  
                  ###O                 
                  | |              | | 
                                   ###O
                                   | | 

我尝试了以下算法,但是在将形状与文本文件中的另一个形状匹配时遇到很多困难

这是我的审判

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


int nthOccurrence(std::string str, const std::string& findMe, int nth)
{
    size_t  pos = 0;
    int     cnt = 0;
    std::string::iterator end_pos = std::remove(str.begin(), str.end(), ' ');
    str.erase(end_pos, str.end());

    while (cnt != nth)
    {
        pos = str.find(findMe, pos);
        pos += 1;

        if (pos == std::string::npos)
            return -1;
        cnt++;
    }
    return pos;
}

bool check_two_clammer(string a)
{   
    if (nthOccurrence(a, "||||", 0))
        return true;
    return false;
}

int check_clammer(string str)
{
    int counter = 0;
    std::string::iterator end_pos = std::remove(str.begin(), str.end(), ' ');
    str.erase(end_pos, str.end());

    int res = !str.compare("||");
    int res_2 = !str.compare("||||");

    if (res == 1)
        return 1;
    else if (res_2 == 1)
        return 4;
    else
    {
        return false;
    }
}

bool check_hashes(string a, string b)
{
    int result = nthOccurrence(a, b, 3);
    return result;
}
int main() {
    ifstream File1;
    ifstream File2;
    ofstream File3;
    string line, line2;


    File1.open("bug.txt");
    File2.open("landscape.txt");
    if (File1.fail()) { cerr << "Error opening file !!" << endl; exit(1); }
    if (File2.fail()) { cerr << "Error opening file !!" << endl; exit(1); }
    std::string lineA;
    std::string lineB;
    int no_bugs = 0;
    bool overlap = false;

    while (std::getline(File1, lineA))
    {
        bool match = false;
        int shape_counter = 0;

        // read File2 until match is found
        while (std::getline(File2, lineB))
        {
            if (lineB.find_first_not_of(' ') != std::string::npos)
            {
                if (check_clammer(lineB) ==1)
                {
                    shape_counter++;

                }
                else if (check_clammer(lineB) == 4)
                {
                    shape_counter++;
                    overlap = true;
                }
                else if (check_hashes(lineB, "###O"))
                {
                    shape_counter++;
                }


                if (shape_counter  == 3)
                {
                    no_bugs++;

                    shape_counter = 0;
                }
            }
            else
            {
                cout << "found empty line" << std::endl;
            }

            // clear the state of File2 stream
            File2.clear();
        }
    }
    if (overlap)
    {
        no_bugs++;
    }
    std::cout << no_bugs << std::endl;
}

我尝试针对以下景观进行解决,但不适用于重叠图案,是否有针对一般景观的算法?

0 个答案:

没有答案