剔除三个(包括三个以上)的子串

#include <iostream>

#include <stdio.h>

using namespace std;

char *str(char *p)
{
    char *p1;
    p1 = (char*)malloc(strlen(p)+1);
    memset(p1,'\0',strlen(p)+1);    
    int count = 0;
    int i,j;
    int x,y;
    int w;
    bool b = 1;

    while(b)
    {
        b = 0;
        w = (signed)strlen(p);
        x=0;
        y=strlen(p);
        for(i = 0; i < w-2; i++)
        {
            if((p[i] == p[i+1]) && (p[i+1] == p[i+2])) 
            {
                x = i;
                y = i+2;
                b = 1;
                for(j = i+3; j < strlen(p); j++ ) 
                {
                if(p[x] == p[j])
                    y = j;
                else break;
                } 
            }  
            if(b ==1) break;
        }
        if(b == 1)
        {
          for(i = 0; i < x; i++)
          {
              p1[i] = p[i];
          }
          for(j = y+1; j < strlen(p); j++)
          {
              p1[i] = p[j];
              i++;
          }
          memset(p,'\0',strlen(p));
          for(i = 0; i < strlen(p1); i++)
          {
              p[i] = p1[i];
          }
          memset(p1,'\0',strlen(p));
        }    
    }
    return p;
}

int main()
{
    char p[] = "daaacbbbacbbbccaa";
    cout<<str(p)<<endl;
}

 

posted @ 2016-09-09 22:27  小德cyj  阅读(240)  评论(0编辑  收藏  举报