problem link: http://codeforces.com/problemset/problem/363/CA simple greedy approach will give you the solution.
A simple greedy approach will give you the solution.
Special care has to be taken for testcases like aabbccddeeff, aaaabbc
Code is self explanatory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <list> | |
#include <map> | |
#include <set> | |
#include <deque> | |
#include <queue> | |
#include <stack> | |
#include <bitset> | |
#include <algorithm> | |
#include <functional> | |
#include <numeric> | |
#include <utility> | |
#include <sstream> | |
#include <iostream> | |
#include <iomanip> | |
#include <cstdio> | |
#include <cmath> | |
#include <cstdlib> | |
#include <ctime> | |
#include <cstring> | |
#include <climits> | |
#include <stdlib.h> | |
#include <stdio.h> | |
using namespace std; | |
#define REP(i,n) for(int i=0; i<n; i++) | |
#define FOR(i,st,end) for(int i=st;i<end;i++) | |
#define db(x) cout << (#x) << " = " << x << endl; | |
#define mp make_pair | |
#define pb push_back | |
#define MAX 10000005 | |
typedef long long int ll; | |
//count in seperate vector and character in seperate vector | |
//count1[i] denotes the count of character[i] | |
vector<int>count1; | |
vector<char>character; | |
int main(){ | |
string str; | |
cin>>str; | |
REP(i,str.size()){ | |
char candidate=str[i]; | |
int c=1; | |
for(int j=i+1;j<str.size();j++){ | |
if(candidate==str[j]) | |
c++; | |
else | |
break; | |
} | |
i+=(c-1); | |
count1.pb(c); | |
character.pb(candidate); | |
} | |
REP(i,count1.size()){ | |
if(count1[i]>=3) | |
count1[i]=2; | |
} | |
REP(i,count1.size()-1){ | |
if(count1[i]==2&&count1[i+1]==2){ | |
count1[i+1]=1; | |
} | |
} | |
REP(i,count1.size()){ | |
REP(j,count1[i]){ | |
cout<<character[i]; | |
} | |
} | |
} |