bestcoder3(1003)hdu4909(状态压缩+乱搞)

String

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 443    Accepted Submission(s): 113


Problem Description
You hava a non-empty string which consists of lowercase English letters and may contain at most one '?'. Let's choose non-empty substring G from S (it can be G = S). A substring of a string is a continuous subsequence of the string. if G contains '?' then '?' can be deleted or replaced by one of lowercase english letters. After that if each letter occurs even number of times in G then G is a good substring. Find number of all good substrings.
 

Input
The input consists of an integer T, followed by T lines, each containing a non-empty string. The length of the string doesn't exceed 20000.

[Technical Specification]
1 <= T <= 100
 

Output
For each test case, print a single integer which is the number of good substrings of a given string.
 

Sample Input
    
    
    
    
3 abc?ca aabbcc aaaaa
 

Sample Output
    
    
    
    
7 6 6
Hint
Good substrings of "abc?ca": "?", "c?", "?c", "c?c", "bc?c", "c?ca", "abc?ca"


题意:就是给一个字符串,该字符串最多含一个问号,然后问你有多少个满足条件的子串,满足条件定义为,该子串里面的所有字符都出现偶数次,如果含有问号,可以将问号替换为任意字符也可以不替换(如果不替换,问号就算作空字符)

思路:由于只有26个小写字母,可以用26位的二进制来压缩子串的状态,那么首先对整个字符串处理,for一遍以后,每个位置都有一个对应的值(将该位置的字符异或到对应的位置上),如果有两个位置对应的值相同,那么这两个位置之间的子串就是满足条件的,可以分两种情况

1.不含问号,那么去掉问号处理一遍即可

2.含问号,那么可以枚举问号为26个字符,再分别处理每一次的情况即可

这题有点卡时,当时被坑了好久~

你可能感兴趣的:(ACM,HDU,BestCoder)