我有两个字符串a
,b
,长度分别为l1
,l2
。我想返回给定两个字符串的常用字母数。例如,a='ABC'
和b='CDE'
。我的算法应返回1
,因为只有'C'
是两个字符串中的公共字母,但它会返回26
。有人可以解释一下原因吗?这是我的算法:
for(i=0; i < l1; i++)
{
for(j=0; j < l2; j++)
{
if(a[i] == b[j])
{
found++;
}
}
}
答案 0 :(得分:0)
您的算法会失败您的标准。如果一个字符串是'CCC',很容易看出会发生什么。
这样做:
int letters[26]={0};
int both[26]={0};
for(int i=0; i<l1; i++)
letters[a[i]-'A']=1;
for(int i=0; i<l2; i++)
if(letters[b[i]-'A'])
both[b[i]-'A']=1;
int found=0;
for(int i=0; i<26; i++)
found+=both[i];
答案 1 :(得分:0)
// With this code I preferred to convert all char as an ascii code.
// Because it is more simpler to compare int value than with `2` for statement
// it has compared if chars are similar, increase the number of `sayi`, then
// print the equal char of these code
int main(){
char a[]="ABC";
char b[]="CDE";
int sizeofa=sizeof(a)-1;
int sizeofb=sizeof(b)-1;
int sayi=0;
int asciia;
int asciib;
for (int i=0; i<sizeofa;i++)
{
for (int j=0; j<sizeofb; j++)
{
asciia=a[i];
asciib=b[j];
if ( asciia == asciib)
sayi++;
}
}
printf("%d",sayi);
}
答案 2 :(得分:0)
示例不完整,因此很难说出为什么会得到26
。可能found
尚未初始化或您的打印输出不正确。
算法本身不正确,因为它会过度计算常见字符。 例如
char *a = "A";
char *b = "AAAA";
第二个循环将计算'A'
个字符4
次。
for(j=0;j<l2;j++)
{
if(a[i]==b[j])
你必须避免过度计算,例如看看:
size_t count_common_chars(const char *s1, const char *s2) {
size_t counter = 0; // our occurrence counter
int index[256] = {0}; // clear the index table
while(*s1){ // for every character in the string s1
index[*s1]++; // use string s1 characters as index to index table s1 = "aaa" would yield index['a'] == 3
s1++; // advance to next char
}
// For every character in the string s2 check if character existed in s1.
// If yes increase the counter.
//
// If a particular character occurs 'n' times in string s1
// then the number of common characters in string s2 cannot be greater than 'n'.
// That is why we have to subtract the number of occurrences of a given character 'X' in string s2
// from the reference index table which counted the occurrence of the same character in string s1.
//
// This is a generic approach, we would do subtraction whether or not there is a match.
// At the end the index table contains a count of a differences between s1 and s2.
// Positive number at the specific index means that there was more such characters in string s1
// Negative number at the specific index means that there was more such characters in string s2
// 0 means that number of particular character was the same or there was no such character in the strings s1 and s2
while(*s2){ // for every character in the string s2
if ( (index[*s2]--) > 0)
counter++;
s2++;
}
return counter;
}
答案 3 :(得分:-4)
It's return 1
char a[3]={'a','b','c'};
char b[3]={'c','d','e'};
int found=0,l1=3,l2=3;
for(int i=0;i<l1;i++)
{
for(int j=0;j<l2;j++)
{
if(a[i]==b[j])
{
found++;
}
}
}