String
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 781 Accepted Submission(s): 213
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
[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 6HintGood substrings of "abc?ca": "?", "c?", "?c", "c?c", "bc?c", "c?ca", "abc?ca"
c++提交TLEg++提交AC
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<list>
#include<queue>
#include<map>
using namespace std;
const int maxn=20010;
char str[maxn];
map<int,int>state;
int main()
{
int t,i,j,k,pos;
scanf("%d",&t);
while(t--){
scanf("%s",str);
int len=strlen(str);
for(i=0;i<len;++i){
if(str[i]=='?')break;
}
state.clear();
int ans=0;pos=i;
if(pos>=len){
int p=0;
state[0]++;
for(i=0;i<len;++i){
p^=(1<<(str[i]-'a'));
ans+=state[p];
state[p]++;
}
}
else {
int p=0;
state[p]++;
for(i=0;i<pos;++i){
p^=(1<<(str[i]-'a'));
ans+=state[p];
state[p]++;
}
state.clear();
p=0;state[p]++;
for(i=pos+1;i<len;++i){
p^=(1<<(str[i]-'a'));
ans+=state[p];
state[p]++;
}
p=0;
if(state.count(p))ans+=state[p];
for(i=0;i<26;++i){
if(state.count(p^(1<<i))){
ans+=state[p^(1<<i)];
}
}
for(i=pos-1;i>=0;--i){
p^=(1<<(str[i]-'a'));
if(state.count(p))ans+=state[p];
for(j=0;j<26;++j){
if(state.count(p^(1<<j)))ans+=state[p^(1<<j)];
}
}
}
printf("%d\n",ans);
}
return 0;
}