hdoj4909String【状压+位运算】

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
 

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"
 

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;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值