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串的更多相关文章
- AB串(上帝都不会,我就没救了)
[题目分析] 设答案的长度为m,
- 数据结构(三)串---KMP模式匹配算法
(一)定义 由于BF模式匹配算法的低效(有太多不必要的回溯和匹配),于是某三个前辈发表了一个模式匹配算法,可以大大避免重复遍历的情况,称之为克努特-莫里斯-普拉特算法,简称KMP算法 (二)KMP算法 ...
- Codeforces 划水
Codeforces 566F 题目大意:给定$N$个数,任意两个数之间若存在一个数为另一个数的因数,那么这两个数存在边,求图中最大团. 分析:求一个图最大团为NP-Hard问题,一般不采用硬方法算. ...
- BZOJ 3160: 万径人踪灭
Description 一个ab串,问有多少回文子序列,字母和位置都对称,并且不连续. Sol FFT+Manacher. 不连续只需要减去连续的就可以了,连续的可以直接Manacher算出来. 其他 ...
- 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 ...
- POJ 3415 后缀数组
题目链接:http://poj.org/problem?id=3415 题意:给定2个串[A串和B串],求两个串公共子串长度大于等于k的个数. 思路:首先是两个字符串的问题.所以想用一个'#'把两个字 ...
- poj3415
很久以前写的,忘补结题报告了两串相连中间用特殊的分隔符然后求height,由于要求求公共子串大于等于k的个数,并且只要位置不同即可因此不难想到在名次上对height分组,一组内的height保证> ...
- 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 ...
- UVALive 5792 Diccionário Portuñol
字符串匹配问题 有n个a串个m个b串,讲a的前缀和b的后缀粘在一起有多少个不同的新串. 首先求不同的前缀和后缀肯定好求了,就用字典树分别存一下a个倒过来的b. 那个问题就是解决例如,abcd,和bcd ...
随机推荐
- Nginx实现负载均衡&Nginx缓存功能
一.Nginx是什么 Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambl ...
- javaScript函数提升及作用域
代码片段: var a = 1; function foo() { console.log(a); //输出为undefined if (!a) { var a = 2; } alert(a); }; ...
- map的常用方法
1.头文件: #include<map> 2.定义: map<string,int>Map; 或: typedef map<string,int> MAP; MAP ...
- Android 工程师
转发:https://zhuanlan.zhihu.com/p/30429725 这句话我真的憋了好久.Android 工程师只要关注我,我就能让你达到大师级水平,不是面试时的吹牛逼水平,不是自我欺骗 ...
- ASP.NET Core 依赖注入(DI)简介
ASP.NET Core是从根本上设计来支持和利用依赖注入. ASP.NET Core应用程序可以通过将其注入到Startup类中的方法中来利用内置的框架服务,并且应用程序服务也可以配置为注入. AS ...
- 《天书夜读:从汇编语言到windows内核编程》十 线程与事件
1)驱动中使用到的线程是系统线程,在system进程中.创建线程API函数:PsCreateSystemThread:结束线程(线程内自行调用)API函数:PsTerminateSystemThrea ...
- 重构手法之Introduce Explaining Variable(引用解释性变量)
返回总目录 6.5Introduce Explaining Variable(引用解释性变量) 概要 你有一个复杂的表达式. 将该复杂表达式(或其中一部分)的结果放进一个临时变量,以此变量名称来解释表 ...
- 使用swiper简单的h5下滑翻页效果,
<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" ...
- Adobe阅读器漏洞(adobe_cooltype_sing)学习研究
实验环境:Kali 2.0+Windows XP sp3+Adobe Reader 9.0.0 类别:缓冲区溢出 描述:这个漏洞针对Adobe阅读器9.3.4之前的版本,一个名为SING表对象中一个名 ...
- 负载均衡手段之DNS轮询
大多数域名注册商都支持对统一主机添加多条A记录,这就是DNS轮询,DNS服务器将解析请求按照A记录的顺序,随机分配到不同的IP上,这样就完成了简单的负载均衡.下图的例子是:有3台联通服务器.3台电信服 ...