Displaying the total number of adjacent pairs in this program

天大地大妈咪最大 提交于 2019-12-13 10:13:40

问题


Given an input char[] I would like to find the number of discrete pairs in the string. So for an input of:

  • "dddd" the output is 2 pairs
  • "ddd" the output is 1 pair
  • "dd" the output is 1 pair
  • "d" the output is 0 pairs

The characters in each pair must be adjacent. For an input of "abca" the output is still 0 because the 'a's are not adjacent.*

The goal is to find the total number of pairs in the string. So for an input of "aaxbb" the output should be 2.*

For my input string of char a[] = "dppccddd" there are 3 pairs of adjacent letters but my program's output is 4. How do I solve the problem?

int i = 0, count = 0, count1 = 0;

for (i = 0; i <= 6; i++)
{
    if (a[i] == a[i + 1])
    count++;
}
printf("%d", count);

回答1:


Just to make your code slightly better, instead of hardcoding value of 6, use for(i = 0; i < sizeof(a) / sizeof(a[0]) - 1; i++) To get a number of elements in an array.

The problem with your code is that if two chars are matched, it will start comparing from the second one again, you need to skip that character, so increase i by 1:

if(a[i] == a[i + 1]) {
    ++count;
    ++i;
}



回答2:


For adjacent cells:

All you have to do is jump by one additional step if you find a pair, as @Cool Guy mentioned:

if(a[i]==a[i+1]) {count++; i++;}


For not only adjacent cells:

The easiest way to solve the case when you want to compare not only adjacent cells is to count every character frequency in a separate array and then check if those frequencies are divisable by 2.

How to build an array of frequencies?

If you only store a-z letters in your first array, the second one needs to have 26 cells (for a-z letters). First: set all cells of freq[26] to 0. Then, for every cell in the first array a[], increment valid cell:

freq[0]++; // i found letter 'a'
freq[4]++; // i found letter 'e'

How to know which cells in freq[] should I increment?

Pro-tip: you can find out which cell to increment by writing freq[ a[i]-'a' ]++;. Why?

If a[i] is 'a', then a[i]-'a' gives you 0,
if a[i] is 'c', then a[i]-'a' gives you 2 etc.




回答3:


Do not use a for loop

Since you'll be implementing a greedy approach, you need more control on the indexing variable, therefore use a while loop instead.

Increment your index variable

For a correct "exclusive pairs" counting, you need to increment it each time a pair is found.

Beware of edge cases

You have to iterate until sizeof(a) - 1, because you don't want to fall out of index.

Keep it simple and succinct

Time complexity: O(n). Space complexity: O(1).

int count = 0;
int i = -1;
char a[] = "dppccddd";

while(++i < sizeof(a) - 1)
{
    if (a[i] == a[i + 1])
    {
        count++;
        i++;
    }
}
printf("Pairs count is: %d", count);

Live demo.




回答4:


The answers to this question given by Amir and Mateusz Kwasniak are correct but they both segfault on zero-sized as: http://ideone.com/BoxocM

A better solution is to control the increment and look behind not ahead:

int i = 1;
int count = 0;
char a[] = "dppccddd";

while(i < sizeof(a) / sizeof(a[0])) {
    if(a[i - 1] == a[i]) {
        ++count;
        i += 2;
    } else {
        ++i;
    }
}

printf("%d", count);

[Live Example]

This will not segfault on any size a, and will provide identical results on non-zero sized as.



来源:https://stackoverflow.com/questions/32692645/displaying-the-total-number-of-adjacent-pairs-in-this-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!