hdu 4909 String (map + 状压)

题目大意:

给定一个可能含‘?’的字符串。然后问这个字符串有多少个子串是含有所有的字符都只出现两次。

其中‘?‘ 可以被替换成任意字符,也可以被remove...

思路分析:

这是bestcoder的round #3的第三题。

这道题的做法和 4908 的做法差不多。

我们把 ‘?’ 左右两边的状态分别处理出来。

然后用map 计数。然后枚举左边的状态。同时枚举? 对应的字符。

然后去寻找右边对应的状态,此时 ans 就可以加上答案。

注意的是要考虑到以 ? 结尾的情况。所以在 ? 的状态要和上一个相同。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#define maxn 20005

using namespace std;

char str[maxn];
int st[maxn];
map<int,int>lef;
map<int,int>rig;
map<int,int>mp;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        lef.clear(),rig.clear(),mp.clear();

        scanf("%s",str+1);

        int pos=0;
        int len=strlen(str+1);

        for(int i=1;i<=len;i++)
        {
            if(str[i]=='?'){
                pos=i,st[i]=st[i-1];
            }
            else {
                st[i]=(st[i-1]^(1<<(str[i]-'a')));
            }
        }

        for(int i=0;i<=len;i++){
            mp[st[i]]++;
            if(pos && i<pos){
                lef[st[i]]++;
            }
            if(pos && i>=pos){
                rig[st[i]]++;
            }
        }
        int ans=0;
        if(pos){
            for(map<int,int>::iterator it = lef.begin();it!=lef.end();it++){
                int x=it->first;
                for(int j=0;j<26;j++){
                    int y=(x^(1<<j));
                    map<int,int>::iterator ct = rig.find(y);
                    if(ct!=rig.end())
                    {
                        ans+=(it->second)*(ct->second);
                    }
                }
            }
        }
        for(map<int,int>::iterator it = mp.begin();it!=mp.end();it++)
            ans+=(it->second*(it->second-1))/2;
        printf("%d\n",ans);
    }
    return 0;
}

hdu 4909 String (map + 状压)

时间: 2024-08-06 20:02:34

hdu 4909 String (map + 状压)的相关文章

[BestCoder Round #3] hdu 4909 String (状压,计数)

String 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 th

HDU 4909 String 统计+状压

因为连续异或满足区间减法性质,所以可以状压之后用异或来判断是否为符合条件的单词并且存储次数 一开始用map,一直超时.虽然直接用开1<<26的数组内存存的下,但是memset的时间肯定会超,但是只要在每次循环之后把加过的值减掉就可以绕过memset了. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits>

hdu 2825 aC自动机+状压dp

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5640    Accepted Submission(s): 1785 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

hdu 3247 AC自动+状压dp+bfs处理

Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 2382    Accepted Submission(s): 750 Problem Description Great! Your new software is almost finished! The only thing left to

HDU 4924 Football Manager(状压DP)

题目连接 : http://acm.hdu.edu.cn/showproblem.php?pid=4924 题意 : n(<=20)个人选出11个人作为首发组成a-b-c阵容,每个人都有自己擅长的位置,并且每个人和其他人会有单向的厌恶和喜欢关系,每个人对于自己擅长的位置都有两个值CA和PA,有喜欢或者厌恶关系的两个人在一起也会影响整个首发的CA总值,要求选出一套阵容使得CA最大,或者CA一样的情况下PA最大. 思路 : 状压搞,dp[s]s的二进制表示20个人中选了那几个人,然后规定选进来的顺序

HDU 4909 String(组合数学)

HDU 4909 String 题目链接 题意:给定一个字符串全是小写字符,可能有一个位置为?,问号可以替代任何字符,也可以删掉,问有多少连续字串满足所有字母是偶数个 思路:组合数学,计算所有前最串的各个字母的奇偶状态,用一个01串表示,然后记录下个数,对于每个相同的状态,任选两个就能得到一个子序列,答案为所有C(num, 2)的和. 但是这个问题多了一个?的情况,但是没关系,可以枚举?,然后把序列分为3部分去考虑,?之前,?之后,和包含了?的串分开考虑即可 代码: #include <cstd

HDU 1074 Doing Homework(状压DP)

Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will r

hdu4921 Map(状压统计)

hdu4921 Map(状压统计) 题意:有10条长度不超过1000链,链上的节点有权值.我们从这些节点中选出一些节点来,若要选节点u,则u的前继都得被选进去.对于某一种选定的情况,我们能获得的权值为,选定的节点的权值和,以及一些附加值.附加值的求法为,对于每条链的同一深度的点,若选定的点的个数超过1,那么会得到的附加值为(si*xi/ci),其中si表示该层选中的点的权值和,xi为该层选中的点的个数,ci为该层的总点数.问,对于所有的选择情况,能得到的权值期望会是多少? 解法:很直观的一点,期

hdu 4909 String(计数)

题目链接:hdu 4909 String 题目大意:给定一个字符串,由小写字母组成,最多包含一个问号,问号可以表示空或者任意一个字母.问有多少个子串,字母出现的次数均为偶数. 解题思路:因为最多又26个字母,对应每个字母的奇数情况用1表示,偶数情况用0.将一个前缀串表示成一个二进制数.然后对于每种相同的数s,任选两个即为一种可行子串(组合数学). 接着对于有问号的情况枚举一下问号替代的字符,然后对于问号后面的状态都要再加上一个该字符.这时计算个数时就要将前后分开讨论了. 这题交C++,结果卡FS