[英]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.