AB串

题目:

给定n个A和2n个B,用这些字符拼成一个字符串,要求这个串的所有前缀和后缀B的个数始终不少于A。

(一个字符串的前缀是只从开头到某个位置为止的子串,后缀是只从某个位置到结尾的子串)。

输入格式

多组数据,每组数据只有一行,包含一个正整数n。(n<=10^17)。

输出格式

每组数据输出一行,最终结果对99991取余数的结果。

分析:

简单的想法是建立一个二叉树,深度遍历一下即可。但是效率太低,对比较大的n不太适用,暂时没想到什么好办法,先简单的做做看。

代码:

<span style="font-size:18px;">#include <stdio.h>
#include <stdlib.h>

struct Node {
	int value;
	struct Node *left;
	struct Node *right;
};

#define n 3
static int result = 0;
static int index = 0;
static int array[3 * n] = {0};

struct Node* initNode()
{
    struct Node* temp = (struct Node *)malloc(sizeof(struct Node));
    temp->value = 0;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}

void creatTree(struct Node* parent, int a, int b)
{
    if (a > 0)
    {
        struct Node* left = initNode();
        left->value = -1;
        parent->left = left;
        creatTree(left, a - 1, b);
    }
    if (b > 0)
    {
        struct Node* right = initNode();
        right->value = 1;
        parent->right = right;
        creatTree(right, a, b - 1);
    }
    if (a <= 0 && b <=0)
    {
        return;
    }
}

//static int array[100] = {0};

void dfs(struct Node* root, int sum)
{
    int i = 0;
    if (root == NULL)
    {
        //result++;
        return;
    }

    if (root->value == 1 && root->left == NULL && root->right == NULL)
    {
        result++;
        printf("%d\n", root->value);
        printf("\nget one\n");
        return;
    }

    sum += root->value;

    if (sum < 0 || sum > n)
    {
        return;
    }
    else
    {
        printf("%d\n", root->value);
        //index = 0;
        dfs(root->left, sum);
        dfs(root->right, sum);
    }

}

void freeNode(struct Node* root)
{
    if (root != NULL)
    {
        //printf("%d\n", root->value);
        //printf("%d\n", root->left->value);
        freeNode(root->left);
        freeNode(root->right);
        free(root);
    }
    return;
}

int main()
{
    int a = 0, b = 0, sum = 0;
    a = n;
    b = n * 2;

    /* 根节点 B */
    struct Node *root = initNode();
    root->value = 1;

    /* 建立二叉树 */
    creatTree(root, a, b-1);

    /* 深度搜索遍历法得到结果 */
    dfs(root, sum);
    printf("result = %d\n", result);

    /* 释放内存 */
    freeNode(root);
    return 0;
}
</span>

AB串

时间: 2024-08-04 06:59:19

AB串的相关文章

Codeforces 526D Om Nom and Necklace kmp+hash

题目链接:点击打开链接 题意: 给出长度为n的字符串,常数k 下面一个长度为n的字符串. 问: for(int i = 1; i <= n; i++){ 字符串的前i个字符 能否构成 形如A+B+A+B+A+B+A的形式,其中A有k+1个,B有k个 A和B是2个任意的字符串(也可以为空串) 若可以构成则输出1,否则输出0 } 思路: POJ1961 先用kmp求一个前缀循环节,. 我们观察 ABABABA => AB, AB, AB, A 所以前缀循环节有K个,而后面的A是尽可能地和AB长度接

hdu 4416 Good Article Good sentence (后缀数组)

题目大意: 给出一个A串和很多个B串,求出A中有多少个子串,是所有的B中没有出现的. 思路分析: 后缀数组的作用很容易的求出来整个串中不同的子串个数. 现在要求的是A中不同的,且在B中没有出现过的. 先把AB 串全部连接,跑一遍suffix array.然后求出有多少个不同的子串. 然后再单独用B 串跑 suffix array.再求出单独在B 中有多少个不同的 子串. 然后结果就是 ans1 - ans2 ... 需要注意的问题就是,连接的时候需要把每一个串后面加一个特殊符.但是求不同串的时候

河老师的新年礼物(尺取法)

河老师的新年礼物 发布时间: 2017年1月1日 15:11   最后更新: 2017年1月1日 15:13   时间限制: 1000ms   内存限制: 256M 描述 河老师的新年礼物是一个长度为n的ab串,他想要找出最长的一个子串使得这个子串中每个字符都相等,他称之为“最优子串”.当然对河老师来说这个问题太简单了,于是他加了一个条件:他可以改变这个串中的某些字符,但一次只能改变一个字符,最多能改变k次.河老师想要知道,在可以对串进行改变的前提下,这个ab串的“最优子串”的长度是多少. 输入

Codeforces Round #324 (Div. 2) (快速判断素数模板)

蛋疼的比赛,当天忘了做了,做的模拟,太久没怎么做题了,然后C题这么简单的思路却一直卡到死,期间看了下D然后随便猜了下,暴力了下就过了. A.找一个能被t整除的n位数,那么除了<=10以外,其他都可以用长度为n的10或100,1000 ... 来往上加几个数而得到 #include <iostream> #include <stdio.h> #include <set> #include <algorithm> #include <string.h

codeforce 804B Minimum number of steps

cf劲啊 原题: We have a string of letters 'a' and 'b'. We want to perform some operations on it. On each step we choose one of substrings "ab" in the string and replace it with the string "bba". If we have no "ab" as a substring,

codeforces 的20道C题

A - Warrior and Archer CodeForces - 595C n  偶数  然后n个数字 A B 轮流取一个 A让差变小B让差变大 直到最后2 个   求的是最小剩下的差 最后剩下的 L R  相距 n/2    求一下最小的就行 #include <iostream> #include <cstdio> #include <cmath> #include <map> #include <algorithm> #include

[字典树] Codeforces 557E Ann and Half-Palindrome

题意: 给一个长度为5000的ab串,问你第k大的半回文子串是什么 所谓的半回文串就是下标是奇数的位置前后相等就好了. 思路: 首先发现串的长度只有5000,可以做一个类似区间dp的预处理 处理出dp[i][j]代表第i到j子串是不是半回文子串 然后依次把原串的所有子串插入字典树,并在节点标记个数 然后最后dfs一下k个就好了 代码: #include"cstdlib" #include"cstdio" #include"cstring" #in

面试题之算法集锦

有字符串A,B,求取AB字符串中都含有的字符,例如:①A="hello",B="jeesite",那么输出"e",②A="common",B="month",则输出"mno",输出串的顺序没有要求. 思路1: 把A去重得到A1,B去重得到B1,然后对A1,B1分别进行排序,然后遍历较短的字符串的每个字符是否存在于较长的字符串中,存在则输出 问题: 1.思路很简单,基本大家都会这么考虑,但

POJ3415:Common Substrings——题解

http://poj.org/problem?id=3415 给定两个字符串A 和B,求长度不小于k 的公共子串的个数(可以相同). 论文题,和上道题(POJ2774)类似,首先想到现将AB串合并,然后子串可以表示成字符串后缀的前缀,于是我们比较任意两个A后缀和B后缀,用height求出他们的公共子串长度就很好做了. (不懂为什么好做的可以看SPOJ694) 那么最坏的想法就是每遇到B后缀就和前面遇到的A后缀比较,这样显然TLE,也没用到height数组的优越性. 但是我们A后缀可以用单调栈维护