[英]using memset() and memcpy() function
// CPP Program to find all the common characters
// in n strings
#include <bits/stdc++.h>
using namespace std;
const int MAX_CHAR = 26;
void commonCharacters(string str[], int n)
{
// primary array for common characters
// we assume all characters are seen before.
bool prim[MAX_CHAR] = {true};
//memset(prim, true, sizeof(prim));
// for each string
for (int i = 0; i < n; i++) {
// secondary array for common characters
// Initially marked false
bool sec[MAX_CHAR] = { false };
// for every character of ith string
for (int j = 0; str[i][j]; j++) {
// if character is present in all
// strings before, mark it.
if (prim[str[i][j] - 'a'])
sec[str[i][j] - 'a'] = true;
}
// copy whole secondary array into primary
//memcpy(prim, sec, MAX_CHAR);
for(int k=0;k<n;k++)
prim[k] = sec[k];
}
// displaying common characters
for (int i = 0; i < 26; i++)
if (prim[i])
printf("%c ", i + 'a');
}
// Driver's Code
int main()
{
string str[] = { "geeksforgeeks",
"gemkstones",
"acknowledges",
"aguelikes" };
int n = sizeof(str)/sizeof(str[0]);
commonCharacters(str, n);
return 0;
}
我们使用两个大小为26的哈希数组(对于az,其中0为a,而z为25)。 这种方法很简单,如果我们在标记字符之前已经看过一个字符,并且如果我们没有忽略该字符,因为它不是一个常见的字符。 为什么这段代码没有给出期望的输出? 而如果我使用memset(prim,true,sizeof(prim))
代替bool prim[MAX_CHAR] = {true};
用于初始化和memcpy(prim,sec,MAX_CHAR)
而不是for(int k=0;k<n;k++) prim[k] = sec[k];
用于将boolean数组sec []复制到prim []中,效果很好。
警告
bool prim[MAX_CHAR] = {true};
不等同于memset(prim, true, sizeof(prim));
MAX_CHAR
为26,并且您仅使用{true}
给出1值,因此prim[0]
初始化为true,而下25个条目全部初始化为0( false )。 直到数组末尾才使用true 。
但
bool prim[MAX_CHAR] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }
初始化26个条目(如果我算数不错)
当然memcpy(prim,sec,MAX_CHAR)
和for(int k=0;k<n;k++) prim[k] = sec[k];
不相等,因为n是字符串数(4)且不等于MAX_CHAR
(26)
用您的代码执行:
pi@raspberrypi:/tmp $ ./a.out
pi@raspberrypi:/tmp $
用memset或{}
的26 true执行,并用for(int k=0; k<MAX_CHAR; k++)
代替for(int k=0;k<n;k++)
for(int k=0; k<MAX_CHAR; k++)
:
pi@raspberrypi:/tmp $ ./a.out
e g k s pi@raspberrypi:/tmp $
从弗朗索瓦·安德里厄的提案(在下面一个移除的话),以除去该问题有关的整洁的初始化:扭转拘谨的布尔值,所以
void commonCharacters(string str[], int n)
{
// primary array for common characters
// we assume all characters are seen before.
// (false means seen before, reverse logic)
bool prim[MAX_CHAR] = {false};
// for each string
for (int i = 0; i < n; i++) {
// secondary array for common characters
// Initially marked false (standard logic)
bool sec[MAX_CHAR] = { false };
// for every character of ith string
for (int j = 0; str[i][j]; j++) {
// if character is present in all
// strings before, mark it.
if (!prim[str[i][j] - 'a'])
sec[str[i][j] - 'a'] = true;
}
// copy negation of whole secondary array into primary
for(int k=0; k<MAX_CHAR; k++)
prim[k] = !sec[k];
}
// displaying common characters
for (int i = 0; i < 26; i++)
if (!prim[i])
printf("%c ", i + 'a');
}
执行:
pi@raspberrypi:/tmp $ ./a.out
e g k s pi@raspberrypi:/tmp $
但是prim的反向逻辑和sec的标准逻辑可能会造成混淆
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.