首页 > 解决方案 > 用于比较数组的函数太少

问题描述

我正在努力做家庭作业。我试图以多种不同的方式运行此代码。这是作业中的 sitrep - 设计一个算法(使用伪代码),将两个 2-D int 数组作为输入,假设为 2 个黑白图像:initialImage x,其尺寸为 IxJ,以及finalImage y,其尺寸为 IxK。该算法将逐行比较 x 和 y,定义如下。您的算法将采用动态编程方案来比较 X 和 Y,确定每行之间的最小差异。

因为您只使用黑白图像,所以您应该假设每个图像都是一个二维 int 数组,由 2 个可能的值组成:0 或 1,其中 0 代表黑色,1 代表白色。因此,这个 0 和 1 值的二维网格构成了一个二维黑白图像。然后,该图像的每一行只是一个用 0 或 1 填充的一维 int 数组。因此,您必须定义如何测量每行中 0 和 1 字符串之间的差异。

请记住,您将一次在图像中进行一行比较。

首先,将 X1,* 与 Y1, 进行比较。(这里 X1,是图像 X 中的第一行,Y1,* 是图像 Y 中的第一行)。接下来,比较 X2 和 Y2……这些比较中的每一个都需要构建一个 D(距离)矩阵。

在以下示例中,X 的第一行是 X1, ,Y 的第一行是 Y1, = 00110。 *抱歉图片不会加载,但它是两个表。第一个是 X 1 2 3 4 5 Y 1 2 3 4 5 1 0 0 1 1 0 1 0 0 1 1 0 2 1 1 0 0 1 2 0 1 0 0 1 3 0 0 1 1 1 3 1 0 1 1 1

完成 D 矩阵后,最下面一行的最小数字就是该行的最小不匹配。您将将此值分配给变量 minVali。该数字说明 X1,* 行与 Y1,* 行的不同之处。然后,您将对所有行 i 重复此比较,并在完成时将差异汇总到变量 totalDifference = Si minVali。

因此,该算法会将总差异与称为阈值的阈值进行比较。如果总值高于阈值,则图像被声明为不同;否则,它们被声明为相似图像。您可以假设 thresh 变量作为算法的输入提供。

// C++ implementation to find the uncommon 
// characters of the two strings 
#include <bits/stdc++.h> 
using namespace std; 

// size of the hash table 
const int MAX_CHAR = 30; 

// function to find the uncommon characters 
// of the 6 strings 
void findAndPrintUncommonChars(string str1, string str2, string str3, string str4, string str5, string str6) 
{ 
    // mark presence of each character as 0 
    // in the hash table 'present[]' 
    int present[MAX_CHAR]; 
    for (int i=0; i<MAX_CHAR; i++) 
        present[i] = 0; 

    int l1 = str1.size(); 
    int l2 = str2.size(); 
    int l3 = str3.size();
    int l4 = str4.size();
    int l5 = str5.size();
    int l6 = str6.size();

    // for each character of str, mark its 
    // presence as 1 in 'present[]' 
    for (int i=0; i<l1; i++) 
        present[str1[i] - 'a'] = 1; 
    for (int i=0; i<l1; i++) 
        present[str2[i] - 'a'] = 1; 
    for (int i=0; i<l1; i++) 
        present[str3[i] - 'a'] = 1; 

    // for each character of str 
    for (int i=0; i<l4; i++) 
    for (int i=0; i<l5; i++) 
    for (int i=0; i<l6; i++) 
    { 
        // if a character of str2 is also present 
        // in str1, then mark its presence as -1 
        if (present[str4[i] - 'a'] == 1 
            || present[str4[i] - 'a'] == -1) 
            present[str4[i] - 'a'] = -1; 
        else
            present[str4[i] - 'a'] = 2;     

        if (present[str5[i] - 'a'] == 1 
            || present[str5[i] - 'a'] == -1) 
            present[str5[i] - 'a'] = -1; 
        else
            present[str5[i] - 'a'] = 2; 

        if (present[str6[i] - 'a'] == 1 
            || present[str6[i] - 'a'] == -1) 
            present[str6[i] - 'a'] = -1; 
        else
            present[str6[i] - 'a'] = 2;    
    } 

    // print all the uncommon characters 
    for (int i=0; i<MAX_CHAR; i++) 
        if (present[i] == 1 || present[i] == 2 ) 
            cout << (char(i + 'a')) << " "; 
    }       
    // Driver program to test above 
int main() 
{ 
    string str1 = {0, 0, 1, 0, 0};
    string str2 = {1, 1, 0, 0, 1};
    string str3 = {0, 0, 1, 1, 1};
    string str4 = {0, 0, 1, 1, 0};
    string str5 = {0, 1, 0, 0, 1};
    string str6 = {1, 0, 1, 1, 1};
    findAndPrintUncommonChars(str1, str4)
    findAndPrintUncommonChars(str2, str5)
    findAndPrintUncommonChars(str3, str6)
    return 1; 
}

标签: c++arguments

解决方案


推荐阅读