删除重复的字符(给一个字符串,删除连续重复的字符,要求时间复杂度为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! ");
char a[100];
cout<<"please input a String!"<<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;
}

原文地址:https://www.cnblogs.com/hackerl/p/3349918.html