Multi-school joint training A. String "Water" title (shaped Pressure + hash) _ Common skills

Source: Internet
Author: User
Topic Description

Gives a string (1<=n<=100000) with a length of N, asking how many consecutive strings all the letters appear to have an even number of times.


input

The first line is a positive integer t that represents the number of data groups (1 <= T <= 10).

Next T line, each line has a string that contains only lowercase letters.


Output

Each answer output satisfies the number of required strings. Each answer occupies one row.


Sample Input

3
a
aabbcc
abcabc


Sample Output

0
6
1


train of Thought

One of the seniors wrote the puzzle, feeling very detailed, so turned around.

Sweep the string from left to right, using state compression records to the current position of the letters are odd or even, if odd, then 1, otherwise 0.

Just imagine, if you sweep a string, all the odd numbers of letters to a certain state are identical to the odd number of letters to a state, and then all the letters of the substrings between the two states must appear occasionally. (Odd-odd = even)

Since the odd number is expressed in 1 and all the odd numbers are the same, (eg:00010000000100000001000001 and 00010000000100000001000001), then the two states must be compressed.

If the state before which it is swept is ans+=, the number of States before that State has occurred.


Eg:ababab, when swept to the first letter A, the state is 1 (00000000000000000000000001), when the 5th letter A is swept to the state of 1, then the letters between the two states must have occurred occasionally several times.

In the case of a state of 0, that is, the current state, all letters have an even number of times, it is the same as the original (has not begun to sweep the string) of the same state, can be the original starting state with the required continuous substring, so status[0]=1.


Eg:abababab, sweep to fourth letter, state 0, Ans+=status[0], then Ans=1. The successive substrings of the substring of the fourth letter and the substring of the No. 0 letter, that is, the abab, all of the letters appear even several times. ++status[0] (the state is 0 more than the number of States, should add 1).

Sweep to eighth letter, state 0, Ans+=status[0], then ans=3. The substring of the eighth letter is a continuous substring of the substring of the No. 0 letter and the substring ending to the fourth letter, that is, abab, and all the letters appear occasionally several times.

Ps:status open an array about more than 60 million, the empty will timeout, so use map instead.

PS: at the beginning of the game, this problem can be used directly with map, but the data has been strengthened, there should be no heavy sentences, so the original code submission will be timed out.

The solution is to use hash to shorten the time of each lookup, before the seniors are going to use a hash chain to deal with, but I thought of using a map array processing. (Suddenly thought of a very good and well written to solve the Greek conflict method, feel very happy ~)


AC Code

 #include <stdio.h> #include <math.h> #include <string.h> #include < stdlib.h> #include <iostream> #include <sstream> #include <algorithm> #include <set> # Include <queue> #include <stack> #include <map> #include <bitset> #define MAXN 100007 using NAMESP

Ace STD;
typedef long Long LL;
Char str[110000];

map<int,int>sk[maxn];
    void Solve () {memset (sk,0,sizeof (SK));
    for (int i=0; i<maxn; i++) sk[i].clear ();
    int Len=strlen (str);
    int ret=0;
    LL ans=0;
    Sk[0][0]=1;
        for (int i=0; i<len; i++) {int now=str[i]-' a ';
        Ret^= (1<<now);
        Ans+=sk[ret%maxn][ret];
    sk[ret%maxn][ret]++;
} cout<<ans<<endl;
    int main () {int T;
    cin>>t;
        while (t--) {cin>>str;
    Solve ();
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.