QueryreuQ Gym - 101806Q

Statements

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 :

  1. Add a lower case alphabet at the back of S.
  2. 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值