A string is palindrome, if the string reads the same backward and forward. For example, strings like "a", "aa", "appa", "queryreuq" are all palindromes.
For given empty string S, you should process following two queries :
- Add a lower case alphabet at the back of S.
- Remove a character at the back of S.
After processing a query, you should count the number of palindrome substring in S. For string S and integers i, j (1 ≤ i ≤ j ≤ |S|), represents a substring from ith to jth character ofS. You should print out the number of integer pairs (i, j) where is palindrome.
Input
Input consists of two lines.
In the first line, Q, the number of queries is given. (1 ≤ Q ≤ 10, 000)
In the second line, the query is given as string of length Q. ith character Ki denotes the ith query.
Ki is '-' or lower case alphabet ('a', 'b', ..., 'z') (without quotes).
If the character is '-', you should remove a character at the back of S. If the character is lower case alphabet, you should add a character Ki at the back of S.
It is guaranteed that length of S is always positive after the query.
Output
Print out Q space-separated integers in the first line. i-th integer should be the answer of the ith query.
Example
Input
17 qu-uer-ryr-reu-uq
Output
1 2 1 2 3 4 3 4 5 7 5 7 9 11 9 11 13
题目大意:给了一个字符串代表一系列操作,如果当前字符是一个小写字母,那么操作就是把这个字符,加到字符串末尾(这个字符串刚开始是空的),如果是减号,就把末尾字符拿掉一个,然后问在每一次操作后字符串中有多少个回文串。
解题思路:不想解释,看代码吧
#include <bits/stdc++.h>
using namespace std;
vector<int> g[10009];
int main()
{
char str[10009];
g[0].clear();
int top=0,n;
int book[10009];
scanf("%d",&n);
getchar();
for(int i=1; i<=n; i++)
{
scanf("%c",&str[++top]);
if(str[top]=='-')
{
top--;
if(top!=0)
top--;
}
else
{
g[top].clear();book[top]=book[top-1];
for(int j=0; j<(int)g[top-1].size(); j++)
{
int x=g[top-1][j]-1;
if(x>=1&&str[x]==str[top])
g[top].push_back(x),book[top]++;
}
if(top>1&&str[top]==str[top-1])
{
g[top].push_back(top-1);
book[top]++;
}
if(top>2&&str[top]==str[top-2])
{
g[top].push_back(top-2);
book[top]++;
//printf("*%d* ",top-2);
}
}
if(i!=n)
printf("%d ",book[top]+top);
else
printf("%d\n",book[top]+top);
}
return 0;
}