Permutation available for duplicate element

来源:互联网 发布:tango软件好不好 编辑:程序博客网 时间:2024/05/22 08:10
const int N=10;
int pcnt[N], u_cnt;
char s[N],ps[N];

void Permu(char *pch, int *pint){
  char *pch2=pch;
  int *pint2=pint;
  while(pch2<ps+u_cnt){
    swap(pch, pch2), swap(pint, pint2);
    (*pint)--;
    if(*pint==0) Permu(pch+1, pint+1);
    else Permu(pch, pint);
    (*pint)++;
    swap(pch, pch2), swap(pint, pint2); 
    return;
  }
  for(pch2=ps,pint2=pcnt;pch2<=ps+u_cnt;pch2++,pint2++){
    int i=(*pint2);
    while(i>0) cout<<(*pch2);
  }
  cout<<endl;
  return;
}

void Compute(){
  int i,k=0;
  ps[k]=s[0];
  pcnt[k]=1;
  for(i=1;i<N;i++){
    if(s[i]==ps[k])pcnt[k]++;
    else ps[++k]=s[i], pcnt[k]=1;
  }
  u_cnt=k;
  Permu(ps, pcnt);
}
// 12'
原创粉丝点击