题目:

给定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串的更多相关文章

  1. AB串(上帝都不会,我就没救了)

    [题目分析] 设答案的长度为m,

  2. 数据结构(三)串---KMP模式匹配算法

    (一)定义 由于BF模式匹配算法的低效(有太多不必要的回溯和匹配),于是某三个前辈发表了一个模式匹配算法,可以大大避免重复遍历的情况,称之为克努特-莫里斯-普拉特算法,简称KMP算法 (二)KMP算法 ...

  3. Codeforces 划水

    Codeforces 566F 题目大意:给定$N$个数,任意两个数之间若存在一个数为另一个数的因数,那么这两个数存在边,求图中最大团. 分析:求一个图最大团为NP-Hard问题,一般不采用硬方法算. ...

  4. BZOJ 3160: 万径人踪灭

    Description 一个ab串,问有多少回文子序列,字母和位置都对称,并且不连续. Sol FFT+Manacher. 不连续只需要减去连续的就可以了,连续的可以直接Manacher算出来. 其他 ...

  5. Hackerrank11 LCS Returns 枚举+LCS

    Given two strings,  a and , b find and print the total number of ways to insert a character at any p ...

  6. POJ 3415 后缀数组

    题目链接:http://poj.org/problem?id=3415 题意:给定2个串[A串和B串],求两个串公共子串长度大于等于k的个数. 思路:首先是两个字符串的问题.所以想用一个'#'把两个字 ...

  7. poj3415

    很久以前写的,忘补结题报告了两串相连中间用特殊的分隔符然后求height,由于要求求公共子串大于等于k的个数,并且只要位置不同即可因此不难想到在名次上对height分组,一组内的height保证> ...

  8. Codeforces - ZeptoLab Code Rush 2015 - D. Om Nom and Necklace:字符串

    D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. UVALive 5792 Diccionário Portuñol

    字符串匹配问题 有n个a串个m个b串,讲a的前缀和b的后缀粘在一起有多少个不同的新串. 首先求不同的前缀和后缀肯定好求了,就用字典树分别存一下a个倒过来的b. 那个问题就是解决例如,abcd,和bcd ...

随机推荐

  1. 前端基于react,后端基于.net core2.0的开发之路(1) 介绍

    文章提纲目录 1.前端基于react,后端基于.net core2.0的开发之路(1) 介绍 2.前端基于react,后端基于.net core2.0的开发之路(2) 开发环境的配置,注意事项,后端数 ...

  2. Linux常用配置讲解

    本文主要讲解Linux的用户设置.主机名设置.网络配置.防火墙配置 用户传输包的命令lrzsz的安装以及SSH服务配置等基本操作. 1. 用户名设置 服务肯定是为了用户,而用户可能对于Linux并不了 ...

  3. 继承JFrame,只是初步

    import java.awt.*; import javax.swing.*; import javax.swing.JFrame; import java.awt.event.WindowList ...

  4. [转载] Netty教程

    转载自http://blog.csdn.net/kobejayandy/article/details/11493717 先啰嗦两句,如果你还不知道Netty是做什么的能做什么.那可以先简单的搜索了解 ...

  5. Spark之MapReduce原理

    参考http://www.cnblogs.com/wuyudong/p/mapreduce-principle.html MapReduce   我们来拆开看: Mapping(映射)对集合里的每个目 ...

  6. js的数组方法整理

    slice 从已有的数组中返回选定的元素.该方法不会修改数组,而是返回一个子数组. 语法:arr.slice(start,end) start: 必须,规定从何处开始选取.如果是负数,就是从尾部开始算 ...

  7. Windows下pycharm远程连接服务器调试-tensorflow无法加载问题

    最近打算在win系统下使用pycharm开发程序,并远程连接服务器调试程序,其中在import tensorflow时报错如图所示(在远程服务器中执行程序正常): 直观错误为: ImportError ...

  8. JQuery使用笔记

    1.选择器 id选择器: $('#btnShow') class选择器: $('.banner') tag选择器: $('input') 2.常用方法 取 / 设value: $('#btnShow' ...

  9. (一)IDEA工具开第一个springboot应用之helloworld

    (一)IDEA工具开第一个springboot应用之helloworld 一.前置知识 1.maven相关知识 2.spring注解 3.RESTful API 二.idea开发第一个springbo ...

  10. 了解python,利用python来制作日常猜拳,猜价小游戏

    初次接触python,便被它简洁优美的语言所吸引,正所谓人生苦短,python当歌.python之所以在最近几年越发的炽手可热,离不开它的一些特点: 1.易于学习:Python有相对较少的关键字,结构 ...