hdu 4909 String (map + 狀壓),hdu4909

來源:互聯網
上載者:User

hdu 4909 String (map + 狀壓),hdu4909

題目大意:

給定一個可能含‘?’的字串。然後問這個字串有多少個子串是含有所有的字元都只出現兩次。

其中'?' 可以被替換成任一字元,也可以被remove...


思路分析:

這是bestcoder的round #3的第三題。

這道題的做法和 4908 的做法差不多。

我們把 ‘?’ 左右兩邊的狀態分別處理出來。

然後用map 計數。然後枚舉左邊的狀態。同時枚舉? 對應的字元。

然後去尋找右邊對應的狀態,此時 ans 就可以加上答案。

注意的是要考慮到以 ? 結尾的情況。所以在 ? 的狀態要和上一個相同。


#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <map>#define maxn 20005using 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;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.