Hdu 4909 String (count), hdu4909

Source: Internet
Author: User

Hdu 4909 String (count), hdu4909

Link: hdu 4909 String

Given a string consisting of lowercase letters, it can contain at most one question mark. A question mark can indicate null or any letter. Ask how many substrings are there, and the number of occurrences of letters is an even number.

Solution: because up to 26 letters correspond to the odd number of each letter, 1 indicates the even number, and 0 indicates a prefix string as a binary number. Then, for each of the same number s, either of them is a feasible substring (combined mathematics ). then, if there is a question mark, enumerate the replacement characters of the question mark, and then add this character to the status after the question mark. In this case, we will discuss the numbers separately.
This question is handed over to C ++, and the result is stuck to the FST. I think it is not scientific. I only need to add the sorting complexity (nlogn), and then it will be called G ++, however, it seems that a 1 <26 array is used to calculate the number, and the space limit is too loose.

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef int ll;const int maxn = 20005;int n, num[maxn], s[maxn];char str[maxn];ll solve () {    int tmp = num[0] = 0;    for (int i = 1; i <= n; i++) {        if (str[i] != '?')            tmp ^= (1<<(str[i]-'a'));        s[i] = num[i] = tmp;    }    ll ret = 0;    sort(num, num + n + 1);    int mv = 0;    while (mv <= n) {        int p = 0;        while (p + mv <= n && num[p+mv] == num[mv])            p++;        ret += p * (p-1) / 2;        mv += p;        }    return ret;}int a[maxn], b[maxn];ll handle (int x, int pos) {    int A = pos, B = n + 1 - pos;    for (int i = 0; i < pos; i++)        a[i] = s[i];    for (int i = pos; i <= n; i++)        b[i-pos] = s[i]^(1<<x);    sort(a, a + A);    sort(b, b + B);    ll ret = 0;    int mva = 0, mvb = 0;    while (mva < A && mvb < B) {        if (a[mva] > b[mvb])            mvb++;        else if (a[mva] < b[mvb])            mva++;        else {            int i = 0, j = 0;            while (i + mva < A && a[i+mva] == a[mva])                i++;            while (j + mvb < B && b[j+mvb] == b[mvb])                j++;            ret += i * j;            mva += i;            mvb += j;        }    }    return ret;}int main () {    int cas;    scanf("%d", &cas);    while (cas--) {        scanf("%s", str+1);        n = strlen(str+1);        int pos = -1;        for (int i = 1; i <= n; i++) {            if (str[i] == '?') {                pos = i;                break;            }        }        ll ans = solve();        if (pos != -1) {            for (int i = 0; i + 'a' <= 'z'; i++)                ans += handle(i, pos);        }        printf("%d\n", ans);    }    return 0;}

HDU 1002

I don't know. I don't know what you wrote. You can check out my code. AC
Code:
# Include <stdio. h>
# Include <string. h>
Int main ()
{
Char a [1, 5001], B [2, 5001];
Int aa [5001], bb [5001], k, n = 0;
Scanf ("% d", & k );
For (int c = 1; c <= k; c ++)
{
Scanf ("% s", a, B );
Int alen = strlen ();
Int blen = strlen (B );
Memset (aa, 0, sizeof (aa); // clear the value in aa and set it to 0.
Memset (bb, 0, sizeof (bb ));
Int maxlen = blen;
If (alen> blen) maxlen = alen;
For (int I = alen-1; I> = 0; I --)
Aa [alen-I] = a [I]-'0 ';
For (int I = blen-1; I> = 0; I --)
Bb [blen-I] = B [I]-'0 ';
For (int I = 1; I <= maxlen; I ++)
{
Aa [I] + = bb [I];
If (aa [I]> 9)
{
If (I = maxlen) maxlen ++;
Aa [I + 1] ++;
Aa [I]-= 10;
}
}
Printf ("Case % d: \ n", ++ n );
Printf ("% s + % s =", a, B );
For (int I = maxlen; I> = 1; I --)
Printf ("% d", aa [I]);
If (k = c)
Printf ("\ n ");
Else
Printf ("\ n ");
}
Return 0;
}

Why am I wrong? Hangzhou electric ACM1004http: // acmhdueducn/showproblemphp? Pid = 1, 1004

Logic error.
Inital_num ();
Put it in while (ballnum! = 0). It cannot be placed in a loop.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.