洛谷 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是个喜欢标新立异的科学怪人.他不使用阿拉伯数字计数,而是使用小写英文字母计数,他觉得这样做,会使世界更加丰富多彩.在他的计数法中,每个 ...
随机推荐
- IntelliJ IDEA详情
详情请参考http://www.phperz.com/article/15/0923/159043.html
- SpringBoot之修改单个文件后立刻生效
问题: 在使用SpringBoot进行开发时,如果修改了某个文件比如前端页面html,不能立刻起效. 解决: 在idea中打开修改后的文件,使用快捷键Ctrl+Shift+F9 进行重新编译,然后刷新 ...
- Unable to resolve target 'android-15'
SDK 15没有加载造成的,在SDK Manager.exe下安装以下文件 Android SDK Tools (25.2.5) Android SDK Platform-tools (28.0.1) ...
- over-relaxation
逐次超松弛sor 参考1https://blog.csdn.net/lusongno1/article/details/68941137 有各种对比和程序 主要就是取了加权平均,没仔细看
- poj-2337(欧拉回路输出)
题意:给你n个字符串,每个字符串可以和另一个字符串连接的前提是,前一个字符串的尾字符等于后一个字符串的首字符,问你存不存在欧拉通路并输出 解题思路:基本标准流程,建图:把一个字符串可以看作一条首字符指 ...
- 炎黄流程中改流程节点颜色的js
- 【BZOJ1211】【HNOI2004】树的计数 prufer序列
题目描述 给你\(n\)和\(n\)个点的度数,问你有多少个满足度数要求的生成树. 无解输出\(0\).保证答案不超过\({10}^{17}\). \(n\leq 150\) 题解 考虑prufer序 ...
- 【BZOJ1426】收集邮票 期望DP
题目大意 有\(n\)种不同的邮票,皮皮想收集所有种类的邮票.唯一的收集方法是到同学凡凡那里购买,每次只能买一张,并且买到的邮票究竟是\(n\)种邮票中的哪一种是等概率的,概率均为\(\frac{1} ...
- IDEA如何查看maven的依赖结构
打开方式: 方法一:该工具有个Maven Projects窗口,一般在右侧能够找到,如果没有可以从菜单栏打开:View>Tool Windows>Maven Projects:选择要分析的 ...
- Hdoj 1879.继续畅通工程 题解
Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计 ...