Skip to content

Instantly share code, notes, and snippets.

@shininglion
Created March 12, 2020 09:18
Show Gist options
  • Save shininglion/d31ee2b0570a183d83d0032f0e3ba2ba to your computer and use it in GitHub Desktop.
Save shininglion/d31ee2b0570a183d83d0032f0e3ba2ba to your computer and use it in GitHub Desktop.
Accepted implementation for the problem 1321C in codeforces
/*
* =====================================================================================
*
* Filename: 1321C.cpp
*
* Description: codeforces 1321C. Remove Adjacent
*
* Version: 1.0
* Created: 2020年03月12日
* Revision: none
* Compiler: gcc
*
* Author: lionking
* Organization: None
*
* =====================================================================================
*/
#include <stdio.h>
#define MAX_S 128
int main()
{
int len, idx = 0, result = 0;
char s[2][MAX_S] = {"", ""};
scanf("%d\n", &len);
fgets(s[idx]+1, MAX_S, stdin);
for (char c = 'z'; c >= 'a'; ) {
for (int i = 1; i <= len; ++i) {
if (s[idx][i] != c) { continue; }
if ((c - s[idx][i-1] == 1) || (c - s[idx][i+1] == 1)) {
s[idx][i] = 0;
}
}
int new_len = 0;
for (int i = 1; i <= len; ++i) {
if (s[idx][i] == 0) { continue; }
s[idx ^ 1][++new_len] = s[idx][i];
}
if (new_len == len) { --c; }
else { result += len - new_len; }
len = new_len;
idx ^= 1;
}
printf("%d\n", result);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment