CareerCup Eliminate all ‘b’ and ‘ac’ in an array of characters

Eliminate all ‘b’ and ‘ac’ in an array of characters, you have to replace them in-place, and you are only allowed to iterate over the char array once. 

Examples: 
abc -> ac 
ac->‘‘ 
rbact->rt

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The key point is in-place and iteration once.


int eliminate( char* p)
{
  int deleted = 0;
  if (! p )
    return deleted;
  while (*p){
    if (*p == ‘b‘)
	  deleted++;
	else if ( ( *p == ‘a‘ ) && ( *(p+1) == ‘c‘)){
	  deleted += 2;
	  p++;
	} 
	else if ( deleted > 0 )
	  *(p-deleted) = *p;
	p++;
  }
  *(p-deleted) = ‘\0‘;
  return deleted;
}


CareerCup Eliminate all ‘b’ and ‘ac’ in an array of characters

上一篇:从几何解释SVD分解


下一篇:poj 3422 Kaka's Matrix Travels (最小费用最大流 模板)