传送门

题解:

  相关变量解释:

 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的计数法的更多相关文章

  1. 洛谷P1061 Jam的计数法

    题目描述 Jam是个喜欢标新立异的科学怪人.他不使用阿拉伯数字计数,而是使用小写英文字母计数,他觉得这样做,会使世界更加丰富多彩.在他的计数法中,每个数字的位数都是相同的(使用相同个数的字母),英文字 ...

  2. 【题解】洛谷 P1061 Jam的计数法

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...

  3. 【洛谷P1061 Jam的计数法】搜索

    分析 超级暴力,在尾部+1,再判断. AC代码 type arr=array[0..27]of longint; var st:string; a:array[0..27]of longint; s, ...

  4. 1140 Jam的计数法

    1140 Jam的计数法 2006年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descri ...

  5. Codevs 1140 Jam的计数法 2006年NOIP全国联赛普及组

    1140 Jam的计数法 2006年NOIP全国联赛普及组 传送门 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Jam是个喜欢标 ...

  6. 洛谷 P1596 [USACO10OCT]湖计数Lake Counting

    题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...

  7. 洛谷P1144 最短路计数(SPFA)

    To 洛谷.1144 最短路计数 题目描述 给出一个N个顶点M条边的无向无权图,顶点编号为1-N.问从顶点1开始,到其他每个点的最短路有几条. 输入输出格式 输入格式: 输入第一行包含2个正整数N,M ...

  8. Jam的计数法

    Jam的计数法 题目描述 Description Jam是个喜欢标新立异的科学怪人.他不使用阿拉伯数字计数,而是使用小写英文字母计数,他觉得这样做,会使世界更加丰富多彩.在他的计数法中,每个数字的位数 ...

  9. Codevs 1140 Jam的计数法

    1140 Jam的计数法 题目描述 Description Jam是个喜欢标新立异的科学怪人.他不使用阿拉伯数字计数,而是使用小写英文字母计数,他觉得这样做,会使世界更加丰富多彩.在他的计数法中,每个 ...

随机推荐

  1. memcach 命令行

    1. cmd上登录memcache # telnet 127.0.0.1 11211   2. 列出所有items stats items     3. 通过itemid获取key 接下来基于列出的i ...

  2. js正則表達式

    正則表達式實例化的兩種方式: 字符型 var a=// 對象型var a=new RegExp(,) 修飾符: i:忽略大小寫 g:全局搜索 m:多行搜索 元字符: \轉義字符 \w:字符,數字,下劃 ...

  3. linux按时间查询日志

    在系统应用集中部署的时候,很多日志因为太多难以定位,获取某段时间的日志是对运维人员非常关键的事情. 一.sed查看某时间段到现在的系统日志: sed  -n  '/May 20 17/,$p'   / ...

  4. Span<T>

    Introduction Span<T> is a new type we are adding to the platform to represent contiguous regio ...

  5. Overrid Equals Defined Operator

    public class Common { public override int GetHashCode() { return base.GetHashCode(); } public overri ...

  6. Using MongoDB with Web API and ASP.NET Core

    MongoDB is a NoSQL document-oriented database that allows you to define JSON based documents which a ...

  7. 监听导航新增Tab选项卡-layui

    1. 加载element模块 2. 监听导航事件 3. 创建选项卡 //加载element模块 layui.use('element', function () { element = layui.e ...

  8. [BZOJ 2083] [POI 2010] Intelligence test

    Description 霸中智力测试机构的一项工作就是按照一定的规则删除一个序列的数字,得到一个确定的数列.Lyx很渴望成为霸中智力测试机构的主管,但是他在这个工作上做的并不好,俗话说熟能生巧,他打算 ...

  9. P1601 A+B Problem(高精)

    原题链接 https://www.luogu.org/problemnew/show/P1601 这个题提示的很清楚,并非简单的A+B,单纯的long  long型也不行(不要被样例所迷惑).因为lo ...

  10. win10系统同时安装python2.7和python3.6

    我是先在本机上安装的python3.6.5,因为要学习一个框架,但是这个框架只支持python2,所以我又安装了python2.7.15,并且配置到系统环境变量 环境变量配置了python3.6.5的 ...