洛谷 P1061 Jam的计数法
题解:
相关变量解释:
int s,t,w;
int tot=;//最多输出五组
int maxNum[maxn];//maxNum[i] : i 位置可以达到的最大值
char letter[maxn];
深搜步骤:
(1):预处理出num[ ],maxNum[ ]
for(int i=;i <= w;++i)
num[i]=letter[i-]-'a'+;//将单词转换成数字
for(int i=w;i >= ;--i)
maxNum[i]=t--;//求出i位置可达到的最大值
(2):从w开始往前遍历,找到数值可以变大的位置,在通过Dfs( )求出由于当前位置影响而满足条件的Jam数
int index=w;
while(index >= )//从后往前遍历
{
while(num[index] < maxNum[index])//判断当前位置是否可以变得更大
{
num[index]++;
Dfs(index,index+,num);//求出受当前位置变大的影响的Jam数
}
index--;
}
(3):Dfs( )
void Dfs(int start,int curPos,int num[])
{
if(tot == )
return ;
//curPos == w+1 作用 : 特判index == 1的情况
if(curPos == w || curPos == w+)
{
if(curPos == w)//特判
num[w]=num[w-]+;
while(num[w] <= maxNum[w])//判断w位置的数是否可以变大
{
Print(num);//打印答案
tot++;
if(tot == )
return ;
num[w]++;
}
int prePos=w-;//回溯,查找w位置之前,start位置之后第一个变大的位置
while(prePos > start && num[prePos] == maxNum[prePos])
prePos--;
if(prePos > start)//找到
{
num[prePos]++;
Dfs(start,prePos+,num);
}
return ;
}
num[curPos]=num[curPos-]+;
Dfs(start,curPos+,num);
}
AC代码:
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=; int s,t,w;
int tot=;//最多输出五组
int maxNum[maxn];//maxNum[i] : i 位置可以达到的最大值
char letter[maxn]; void Print(int num[])
{
for(int i=;i <= w;++i)
printf("%c",num[i]-+'a');
printf("\n");
}
void Dfs(int start,int curPos,int num[])
{
if(tot == )
return ;
if(curPos == w || curPos == w+)
{
if(curPos == w)
num[w]=num[w-]+;
while(num[w] <= maxNum[w])
{
Print(num);
tot++;
if(tot == )
return ;
num[w]++;
}
int prePos=w-;
while(prePos > start && num[prePos] == maxNum[prePos])
prePos--;
if(prePos > start)
{
num[prePos]++;
Dfs(start,prePos+,num);
}
return ;
}
num[curPos]=num[curPos-]+;
Dfs(start,curPos+,num);
}
void Solve()
{
int num[maxn];
for(int i=;i <= w;++i)
num[i]=letter[i-]-'a'+;//将单词转换成数组
for(int i=w;i >= ;--i)
maxNum[i]=t--;//求出i位置可达到的最大值
int index=w;
while(index >= )
{
while(num[index] < maxNum[index])
{
num[index]++;
Dfs(index,index+,num);
}
index--;
}
}
int main()
{
scanf("%d%d%d",&s,&t,&w);
scanf("%s",letter);
Solve();
}
Dfs
大神Dfs()精简代码:
https://rainman.blog.luogu.org/solution-p1061
#include<bits/stdc++.h>
using namespace std; int s,t,w,c;
int a[],cnt; inline void output()
{
for(int i=;i<=w;i++)
cout<<(char)('a'+a[i]-);
cout<<endl;
} void dfs(int pos,int step)
{
if(pos==)
return;
if(step == )
return;
if(a[pos] < t && a[pos] < a[pos+]-)
{
a[pos]++;
for(int i=pos+;i<=w;i++)
a[i]=a[i-]+;
output();
dfs(w,step+);
}
else
dfs(pos-,step);
} int main()
{
cin>>s>>t>>w;
fflush(stdin);
while((c=getchar())!=EOF)
{
int temp=c-'a'+;
if(temp>=&&temp<=)
a[++cnt]=temp;
}
a[w+]=0x7f;
dfs(w,);
return ;
}
Dfs
洛谷 P1061 Jam的计数法的更多相关文章
- 洛谷P1061 Jam的计数法
题目描述 Jam是个喜欢标新立异的科学怪人.他不使用阿拉伯数字计数,而是使用小写英文字母计数,他觉得这样做,会使世界更加丰富多彩.在他的计数法中,每个数字的位数都是相同的(使用相同个数的字母),英文字 ...
- 【题解】洛谷 P1061 Jam的计数法
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...
- 【洛谷P1061 Jam的计数法】搜索
分析 超级暴力,在尾部+1,再判断. AC代码 type arr=array[0..27]of longint; var st:string; a:array[0..27]of longint; s, ...
- 1140 Jam的计数法
1140 Jam的计数法 2006年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descri ...
- Codevs 1140 Jam的计数法 2006年NOIP全国联赛普及组
1140 Jam的计数法 2006年NOIP全国联赛普及组 传送门 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Jam是个喜欢标 ...
- 洛谷 P1596 [USACO10OCT]湖计数Lake Counting
题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...
- 洛谷P1144 最短路计数(SPFA)
To 洛谷.1144 最短路计数 题目描述 给出一个N个顶点M条边的无向无权图,顶点编号为1-N.问从顶点1开始,到其他每个点的最短路有几条. 输入输出格式 输入格式: 输入第一行包含2个正整数N,M ...
- Jam的计数法
Jam的计数法 题目描述 Description Jam是个喜欢标新立异的科学怪人.他不使用阿拉伯数字计数,而是使用小写英文字母计数,他觉得这样做,会使世界更加丰富多彩.在他的计数法中,每个数字的位数 ...
- Codevs 1140 Jam的计数法
1140 Jam的计数法 题目描述 Description Jam是个喜欢标新立异的科学怪人.他不使用阿拉伯数字计数,而是使用小写英文字母计数,他觉得这样做,会使世界更加丰富多彩.在他的计数法中,每个 ...
随机推荐
- 死锁问题分析(个人认为重点讲到了gap间隙锁,解决了我一些不明报死锁的问题)
线上某服务时不时报出如下异常(大约一天二十多次):“Deadlock found when trying to get lock;”. Oh, My God! 是死锁问题.尽管报错不多,对性能目前看来 ...
- vue & @on-change !== on-change @on-change === @change
vue & @on-change !== on-change @on-change === @change https://jsfiddle.net/Lasx1fod/ i-switch ht ...
- Yii2后台管理系统常规单据模块最佳实践
后台管理系统的常规单据通常包括数据,页面,功能:其中数据,页面,功能又可以细分如下: 分类 二级分类 主要内容 注意事项 例如 数据 数据库迁移脚本 用于数据表生成及转态回滚 1.是否需要增 ...
- js外部调用layui.use中的函数的写法
layui模块化的写法固然不错,但也有让人不适应的一些地方 外部调用函数的写法就让人不太舒服 需要在函数名前面加上window这个前缀,就不太舒服 补充:window前缀,是全局变量的声明方式 如下: ...
- 十分钟了结MySQL information_schema
information_schema数据库是MySQL系统自带的数据库,它提供了数据库元数据的访问方式.感觉information_schema就像是MySQL实例的一个百科全书,记录了数据库当中大部 ...
- 转 JQuery:常用方法一览
出处 :http://www.cnblogs.com/Fooo/archive/2010/02/01/1661157.html 代码 Attribute:$(”p”).addClass(css中定义的 ...
- cuda培训素材
http://www.geforce.cn/hardware/desktop-gpus/geforce-gtx-480/architecture http://cache.baiducontent.c ...
- 双系统windows+linux如何正确删除linux
双系统windows+linux如何正确删除linux 2017年11月16日 10:42:49 dovepym 阅读数:26363 之前在windows的基础上又安装了ubuntu系统16.04 ...
- LIRE图片识别搜索demo--Ubuntu开发
Ubuntu安装shadowsocks客户端/服务端教程 1.安装shadowsocks sudo apt-get update sudo apt-get install python-pip sud ...
- LOJ2980 THUSC2017大魔法师(线段树+矩阵乘法)
线段树每个节点维护(A,B,C,len)向量,操作即是将其乘上一个矩阵. #include<iostream> #include<cstdio> #include<cma ...