Skip to content

Instantly share code, notes, and snippets.

@minhducsun2002
Last active June 20, 2018 03:47
Show Gist options
  • Save minhducsun2002/99e4b06b077d7c87332b801c70db49c4 to your computer and use it in GitHub Desktop.
Save minhducsun2002/99e4b06b077d7c87332b801c70db49c4 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
#define int long int
bool check[10001][10001];
main()
{
int n;
char command;
string str;
int length = 0;
int ans = 0;
cin >> n;
for (int i = 0; i < n; i++)
// for each command
{
cin >> command;
if (command != '-')
// if command = push_back
{
ans++;
// at least a palindromic substring added
check[length][length] = true;
for (int i = 0; i < length - 1; i++)
{
if (check[i + 1][length - 1] // if from i + 1 to length - 1 is palindrome
&&
str[i] == command) // and str[i] == command
{
ans++;
// new palindromic substring exists
check[i][length] = true;
// mark as true;
}
}
if (str[length - 1] == command)
// if end == command -> another substring exists
{
ans++;
check[length - 1][length] = true;
}
str += command;
length++;
}
else
{
command = str[length - 1];
// str.erase(str.length() - 1);
str.pop_back(); ans--;
// at lease a palindromic substr loses
check[length - 1][length - 1] = false;
// mark as nonexistent
for (int i = 0; i < length - 1; i++)
{
if (check[i][length - 1])
{
ans--;
// if a substr contains, mark as no longer exist
check[i][length - 1] = false;
}
}
if (str[length - 1] == command)
{
ans--;
check[length - 2][length - 1] = false;
}
length--;
}
cout << ans << " ";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment