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

本文探讨了在给定字符串中包含至多一个问号的情况下,如何找出所有满足条件的子串,即每个子串中字母出现次数为偶数次。通过二进制压缩状态的方法,简化了状态转换和子串匹配的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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个字符,再分别处理每一次的情况即可

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

#include 
#include 
#include 
#include 
#include 
using namespace std;
#define INF ((1<<31)-1)

int n;
char s[20010];
int ans[20010];
int ha[20010];
int jj[27];
int ii[27];

int A[20010];
int B[20010];

int main()
{
    int t,i,j;

    scanf("%d",&t);

    while(t--)
    {
        memset(s,0,sizeof(s));
        scanf("%s",s);
        n=strlen(s);

        int flag=-1;

        for(i=0;i>1;

        if(flag==-1)printf("%d\n",ans1);

        else {

            d=0;
            temp=0;
            ans[d++]=0;
            for(i=0;iB[i2])i2++;
                else {

                    int cnt1=1;
                    int cnt2=1;
                    while(i1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值