删除重复的字符(给一个字符串,删除连续重复的字符,要求时间复杂度为O(1)……)

  

// singal.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream"
using namespace std;

int main(int argc, char* argv[])
{
// printf("Hello World!\n");
char a[100];
cout<<"please input a String!\0"<<endl;
cin>>a;
cout<<"输入的串是:"<<a<<endl;
for (int i = 0;i<strlen(a);i++)
{
if (a[i] == a[i+1] )
{
if (a[i+1]!=a[i+2])//只有两个重复的字符
{
cout<<a[i];
i++;
}//输出第一个,跳过第二个。
else
{
i++;//三个都是连续的字符,跳过一个。
}
}
else
{
cout<<a[i];//没有连续的,直接输出。
}
}
system("pause");
return 0;
}

 

posted @ 2013-10-03 08:59  呱呱老师  阅读(897)  评论(0编辑  收藏  举报