Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1405105
  • 博文数量: 241
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 2253
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-11 22:27
个人简介

--

文章分类

全部博文(241)

文章存档

2021年(3)

2019年(6)

2018年(1)

2017年(9)

2016年(21)

2015年(50)

2014年(125)

2013年(26)

我的朋友

分类: C/C++

2014-03-21 20:55:48

题目描述:
字符串操作问题,将字符串中出现次数最少的删除,例如:"aa"-->""; add-->"dd"
代码:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4.   
  5. #define MAXLEN 30

  6. char * delChar(char *s,int iLen)
  7. {
  8.     int i;
  9.     /*min表示字符串中字符最少的数目*/
  10.     /*数组nCountTable分别存储当前字符串中存在的字符数目,不存在则为0*/
  11.     unsigned int min,nCountTable[MAXLEN];
  12.     char *pSlow = s;
  13.     char *pFast = s;
  14.     if((s == NULL) || iLen <= 0)
  15.     {
  16.         return NULL;
  17.     }
  18.  
  19.     /*初始化为0*/
  20.     for(i = 0;i < MAXLEN;i ++)
  21.     {
  22.         nCountTable[i] = 0;
  23.     }
  24.     /*统计当前字符串中各个字符的数目*/
  25.     for(i = 0;i < iLen;i ++)
  26.     {
  27.         nCountTable[*(s + i) - 'a'] ++;
  28.     }
  29.     /*把nCountTable数组中第一个不为0的,作为第一个数,用于找出最少的个数*/
  30.     while(nCountTable[i] == 0)
  31.     {
  32.         i ++;
  33.     }
  34.     min = nCountTable[i];
  35.     /*找出字符数目最少的那个数,不存在的不算入其中*/
  36.     for(i = 0;i < MAXLEN;i ++)
  37.     {
  38.         if(nCountTable[i] != 0)
  39.         {
  40.             if(nCountTable[i] < min)
  41.             {
  42.                 min = nCountTable[i];
  43.             }
  44.         }
  45.     }
  46.     /*删除字符串中最少的字符,并且返回*/
  47.     while(*pFast != '\0')
  48.     {
  49.         if(nCountTable[*pFast - 'a'] != min)
  50.         {
  51.             *pSlow = *pFast;
  52.             pSlow ++;
  53.         }
  54.         pFast ++;
  55.     }
  56.     *pSlow = '\0';
  57.   
  58.     return s;
  59. }
  60. int main()
  61. {
  62.     char str[MAXLEN] = {0};
  63.     int iLen;
  64.     char* tmp = NULL;
  65.     scanf("%s",str);
  66.     iLen = strlen(str)/sizeof(char);
  67.     tmp = delChar(str,iLen);
  68.     printf("%s\n",tmp);
  69. }
http://blog.csdn.net/tianmohust/article/details/6844651
阅读(1351) | 评论(0) | 转发(0) |
0

上一篇:OJ-大数求和

下一篇:OJ-密码验证合格程序

给主人留下些什么吧!~~