繁体   English   中英

查找并列出字符串C ++中的所有字符簇

[英]Find and list all char clusters in the string C++

我正在寻找一些帮助,以查找C ++中字符串中的所有字符簇。 确切的任务是:

给定以下“ 2D字符串”(在C ++表达式中):

string text =
  "#################aa##a###c######\n" +
  "####bbbbaaaabbbbbaaaaa###ccc##cc\n" +
  "#o##bbbbaaaabbbbbaaaaa###c#c##cc\n" +
  "#oo#bbbbaeeabbbbbbbbaa##cc#ccccc\n" +
  "#o##bbbbaeeabbbbbaaaaaa#cc#####c\n" +
  "#o##bbbbaaaabbbbbaaaaaa#cc#####c\n";

编写一个程序,计算相同符号>的每个连续区域的面积。 如果两个相等的符号在行或列中是相邻的,则它们属于同一区域。 不要计算换行符(\\ n),它们只是用来形成2D字符串。

主要功能应该是递归的。

提示:使用额外的2D数组标记2D字符串中的每个符号(如果已计算)。 逐行扫描数组,直到找到一个尚未计数的符号。 然后,从此>符号开始运行递归区域计算功能。 继续直到所有符号都标记为已计数。

程序输出看起来(或多或少)如下:

Region of symbols #, area …
Region of symbols a, area …
Region of symbols #, area …
Region of symbols c, area …

我当前的代码如下所示:

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

int cords (string str, int x, int y) {
    int length, i, position, lines = 0, x_max, y_max;
    char symbol;
    length = str.length();
    for (i = 0; i < length; i++) {
        symbol = str[i];
        if (symbol == '\n')
            lines++;
    }
    length -= lines;
    x_max = length / lines;
    y_max = length / x_max;
    position = x - 1 + (y - 1) * x_max + y - 1;

    if (x <= x_max && y <= y_max)
        return position;
}

int clusterMiner (char symbol, string str, int x, int y, int counter, int last) {
    if (x > 32 || y > 6) {
        return counter;
    } else {
        if (str[cords(str, x++, y)] == symbol) {
            counter++;
            return clusterMiner(symbol, str, x++, y, counter, x);
        } else if (str[cords(str, 1, y++)] == symbol) {
            return clusterMiner(symbol, str, 1, y++, counter, x);
        }
    }
}

int main () {
    int length, lines, i, j, k, l, counter;
    string text = // 32 elements per line
    "#################aa##a###c######\n" // 32
    "####bbbbaaaabbbbbaaaaa###ccc##cc\n" // 64
    "#o##bbbbaaaabbbbbaaaaa###c#c##cc\n" // 96
    "#oo#bbbbaeeabbbbbbbbaa##cc#ccccc\n" // 128
    "#o##bbbbaeeabbbbbaaaaaa#cc#####c\n" // 160 
    "#o##bbbbaaaabbbbbaaaaaa#cc#####c\n"; // 192

    counter = clusterMiner('#', text, 1, 1, 0, 0);
    cout << counter;

    return 0;
}

绳索功能只是为了更轻松地与弦的两个维度进行交互。

我不确定下一步该怎么做。 现在,该程序仅在停止在第一个不同的符号处时才计入一些符号,而忽略了与其他节点相连的符号。

谢谢!

首先,不要一直都重新计算x_max和y_max,只需执行一次并将其存储在变量中即可。 然后,您将不得不遍历整个字段:

char get(int x, int y)
{
    // + 1: the newline!!!
    return field[x + y * (x_max + 1)];
}

void countAll()
{
    calculateMaxima();
    // created your visited array now
    for(unsigned int y = 0; y <= y_max; ++y)
    {
        for(int x = 0; x <= x_max; ++x)
        {
            if(!visited[x, y])
            {
                count = 0;
                search(get(x, y), x, y);
                // output count here...
            }
        }
    }
}

每次碰到尚未访问的角色(即新角色)时,我们都会开始新的搜索。 对于每次搜索,我们必须为每个当前位置{x, y}考虑四个邻居:
{x +/- 1, y}{x, y +/- (x_max + 1} (除了边缘的位置,该位置较小)。因此您的搜索可能如下所示:

void visit(char symbol, int x, int y)
{
    if(!visited[x][y] && get(x, y) == symbol)
    {
        ++count;
        ++visited[x][y] = true;
    }
    search(symbol, x, y);
}

void search(char symbol, int x, int y)
{
    if(x > 0)
        visit(x - 1, y);
    if(x < max_x)
        visit(x + 1, y);
    if(y > 0)
        visit(x, y - 1);
    if(y < max_y)
        visit(x, y + 1);
}

现在,我假设count,访问和x / y_max是一些全局变量。 和我们一样,Cleaner为此将编写一个单独的类:

class ClusterMiner
{
    unsigned int count;
    std::string field;
    // ...

    void visit(char symbol, int x, int y);
    void search(char symbol, int x, int y);
    // ...
public:
    void countAll();
};

代码未经测试且不完整,它只会给您必要的提示以找到您的方式...

旁注:如果您具有相同字符的未连接区域,则将这样检测。 如果不希望这样,您可以将结果汇总起来,例如在std::map<char, unsigned int>并在完成计数后对该值进行迭代...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM