另一个数组的方括号内的数组元素 [英] Array element inside the square bracket of another array

查看:91
本文介绍了另一个数组的方括号内的数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

找到最小的窗口在包含另一个字符串的所有字符的字符串中 - GeeksforGeeks [ ^ ]



我无法绕过头脑这段代码。数组中的数组是什么意思?角色怎样才能成为数组的索引?



我尝试过的方法:



 // C ++程序,用于查找包含
//的最小窗口//模式的所有字符。
#include< bits / stdc ++。h>
using namespace std;

const int no_of_chars = 256;

//找到包含
的最小窗口的函数//'pat'的所有字符
string findSubString(string str,string pat)
{
int len1 = str.length();
int len2 = pat.length();

//检查字符串的长度是否小于模式的
//长度。如果是,则不存在这样的窗口
if(len1< len2)
{
cout<< 不存在这样的窗口;
返回;
}

int hash_pat [no_of_chars] = {0};
int hash_str [no_of_chars] = {0};

//存储模式
的字符出现(int i = 0; i< len2; i ++)
hash_pat [pat [i]] ++;

int start = 0,start_index = -1,min_len = INT_MAX;

//开始遍历字符串
int count = 0; //字符数
for(int j = 0; j< len1; j ++)
{
//计算字符串
hash_str的字符出现次数[str [j] ] ++;

//如果string的char与pattern的char
//匹配,则增加count
if(hash_pat [str [j]]!= 0&&
hash_str [str [j]]< = hash_pat [str [j]])
count ++;

//如果所有字符都匹配
if(count == len2)
{
//尝试最小化窗口,即检​​查
//任何角色都在发生更多。
//比它在模式中出现的次数,如果是
//然后从启动中删除它并删除
//无用的字符。
while(hash_str [str [start]]> hash_pat [str [start]]
|| hash_pat [str [start]] == 0)
{

if(hash_str [str [start]]> hash_pat [str [start]])
hash_str [str [start]] - ;
start ++;
}

//更新窗口大小
int len_window = j - start + 1;
if(min_len> len_window)
{
min_len = len_window;
start_index = start;
}
}
}

//如果没有找到窗口
if(start_index == -1)
{
cout<< 不存在这样的窗口;
返回;
}

//从start_index开始返回子串
//和长度min_len
返回str.substr(start_index,min_len);
}

//驱动程序代码
int main()
{
string str =这是一个测试字符串;
string pat =tist;

cout<< 最小的窗口是:\ n
<< findSubString(str,pat);
返回0;
}

解决方案

您必须考虑每个层提供的数据。变量str是一个字符数组。字符只是一个字节的整数。这意味着str [j]的值是一个整数,范围从0到255,即一个字节的范围。该值用于索引hash_pat和hash_str数组,这些数组的长度为256个元素。



如果你扩展这样的表达式,你可能会更清楚:

  for  int  i =  0 ; i< len2; ++ i)
{
int index = pat [i];
++ hash_pat [index];
}

这只是获取内部表达式并将其保存到一个名为index的临时变量,但同样的事情发生在这里。



FWIW,如果这是我的代码,我会更多地使用临时变量。首先,我认为它使调试器中的内容更容易看到。这是一个例子:

  for  int  j =  0 ; j< len1; j ++)
{
int tempstrj = str [j];
// 计算字符串字符的出现次数
hash_str [tempstrj] ++ ;

// 如果字符串的字符与模式的字符匹配
< span class =code-comment> // 然后递增计数
if (hash_pat [tempstrj]!= 0 &&
hash_str [tempstrj]< = hash_pat [tempstrj])
count ++;

// 如果所有字符都匹配
if (count == len2)
{
// 尝试最小化窗口,即检​​查
// 任何字符是发生更多没有。时间
// 比模式中出现的情况,如果是的话
// 然后将其从启动中移除并删除
// 无用的字符。
int tempstrst = str [开始];
while (hash_str [tempstrst]> hash_pat [tempstrst]
|| hash_pat [tempstrst] == 0
{

if (hash_str [tempstrst]> hash_pat [tempstrst])
hash_str [tempstrst] - ;
start ++;
}

// 更新窗口大小
int len_window = j - start + 1 ;
if (min_len> len_window)
{
min_len = len_window;
start_index = start;
}
}
}


引用:

字符如何成为数组的索引?



了解ASCII编码。



你不明白这个代码在做什么,一个工具可以帮助你,它是调试器!



一步一步在调试器上运行你的代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到错误,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。


Find the smallest window in a string containing all characters of another string - GeeksforGeeks[^]

I can't wrap my head around this code. What does array inside an array mean? How can a character be an index to an array?

What I have tried:

// C++ program to find smallest window containing 
// all characters of a pattern. 
#include<bits/stdc++.h> 
using namespace std; 

const int no_of_chars = 256; 

// Function to find smallest window containing 
// all characters of 'pat' 
string findSubString(string str, string pat) 
{ 
	int len1 = str.length(); 
	int len2 = pat.length(); 

	// check if string's length is less than pattern's 
	// length. If yes then no such window can exist 
	if (len1 < len2) 
	{ 
		cout << "No such window exists"; 
		return ""; 
	} 

	int hash_pat[no_of_chars] = {0}; 
	int hash_str[no_of_chars] = {0}; 

	// store occurrence ofs characters of pattern 
	for (int i = 0; i < len2; i++) 
		hash_pat[pat[i]]++; 

	int start = 0, start_index = -1, min_len = INT_MAX; 

	// start traversing the string 
	int count = 0; // count of characters 
	for (int j = 0; j < len1 ; j++) 
	{ 
		// count occurrence of characters of string 
		hash_str[str[j]]++; 

		// If string's char matches with pattern's char 
		// then increment count 
		if (hash_pat[str[j]] != 0 && 
			hash_str[str[j]] <= hash_pat[str[j]] ) 
			count++; 

		// if all the characters are matched 
		if (count == len2) 
		{ 
			// Try to minimize the window i.e., check if 
			// any character is occurring more no. of times 
			// than its occurrence in pattern, if yes 
			// then remove it from starting and also remove 
			// the useless characters. 
			while ( hash_str[str[start]] > hash_pat[str[start]] 
				|| hash_pat[str[start]] == 0) 
			{ 

				if (hash_str[str[start]] > hash_pat[str[start]]) 
					hash_str[str[start]]--; 
				start++; 
			} 

			// update window size 
			int len_window = j - start + 1; 
			if (min_len > len_window) 
			{ 
				min_len = len_window; 
				start_index = start; 
			} 
		} 
	} 

	// If no window found 
	if (start_index == -1) 
	{ 
	cout << "No such window exists"; 
	return ""; 
	} 

	// Return substring starting from start_index 
	// and length min_len 
	return str.substr(start_index, min_len); 
} 

// Driver code 
int main() 
{ 
	string str = "this is a test string"; 
	string pat = "tist"; 

	cout << "Smallest window is : \n"
		<< findSubString(str, pat); 
	return 0; 
} 

解决方案

You have to think about what data each layer provides. The variable str, a string, is an array of characters. A character is just an integer of one byte. That means the value of str[j] is an integer that could range from 0 to 255, the range of one byte. That value is used to index into the hash_pat and hash_str arrays which are 256 elements in length.

It might be more clear for you if you expand the expression like this :

for( int i = 0; i < len2; ++i )
{
    int index = pat[i];
    ++hash_pat[index];
}

That just takes the inner expression and saves it to a temporary variable called index but the same things are happening in this.

FWIW, if this was my code I would be using temporary variables much more. For one thing, I think it makes things easier to see in the debugger. Here's an example :

for (int j = 0; j < len1 ; j++)
{
    int tempstrj = str[j];
    // count occurrence of characters of string
    hash_str[tempstrj]++;

    // If string's char matches with pattern's char
    // then increment count
    if (hash_pat[tempstrj] != 0 &&
        hash_str[tempstrj] <= hash_pat[tempstrj] )
        count++;

    // if all the characters are matched
    if (count == len2)
    {
        // Try to minimize the window i.e., check if
        // any character is occurring more no. of times
        // than its occurrence in pattern, if yes
        // then remove it from starting and also remove
        // the useless characters.
        int tempstrst = str[start];
        while ( hash_str[tempstrst] > hash_pat[tempstrst]
            || hash_pat[tempstrst] == 0)
        {

            if (hash_str[tempstrst] > hash_pat[tempstrst])
                hash_str[tempstrst]--;
            start++;
        }

        // update window size
        int len_window = j - start + 1;
        if (min_len > len_window)
        {
            min_len = len_window;
            start_index = start;
        }
    }
}


Quote:

How can a character be an index to an array?


Learn about ASCII coding.

Your don't understand what this code is doing, a tool can help you to, it the debugger !

Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于另一个数组的方括号内的数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆