题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6223

题意:给定长度为n的一串数字S,现在要按照一种规则寻找长度为n的数字串,使得该数字串的字典序最大。规则:从数字串S的某一个下标为x的数字出发,可以到达的下一个数字是下标为(x*x+1)%n的数字。

思路:BFS+剪枝。剪枝技巧:

1:遍历到某一层的节点时,记录已经到达过的节点,下次如果还经过就直接不考虑。

2:当前遍历到的某一层节点的数字较之前的小,直接不考虑。

AC代码:

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define EPS 1e-7
typedef unsigned long long ll;
const int N_MAX = +;
const int MOD = 1e10+;
int n,st[N_MAX],t;//堆栈st保存当前一层所有的节点的下标
bool vis[N_MAX];
char a[N_MAX],ans[N_MAX],_max;
struct Node {
int index, step;
Node(int index = , int step = ) :index(index), step(step) {}
bool operator < (const Node &b)const {
if (this->step != b.step)return step > b.step;
else return a[this->index] < a[b.index];
}
}; void init(int &n) {
memset(st, , sizeof(n));
t = ;
memset(vis, , sizeof(vis));
for (int i = ; i < n; i++)ans[i] = ''-;
_max = ''-;
} void bfs() {
priority_queue<Node>que;
for (int i = ; i < n; i++) {
if (_max == a[i]) { que.push(Node(i, )); }//初始将值最大的元素节点压入队列
}
int pre_step = -,t=;
while (!que.empty()) {
Node p = que.top(); que.pop();
if (pre_step != p.step) {//判断当前是第几层,如果到达新一层,之前记录销毁
pre_step = p.step;
while (t)vis[st[--t]] = false;
}
if (ans[p.step] > a[p.index] || p.step >= n || vis[p.index])continue;//如果当前的节点value比较小或者当前节点已经走过,剪枝
ans[p.step] = a[p.index];
st[t++] = p.index;
vis[p.index] = true;
que.push(Node(((ll)p.index*p.index+)%n,p.step+));
} } int main() {
int t; scanf("%d",&t);
for (int cs = ; cs <= t;cs++) {
scanf("%d", &n);
init(n);
scanf("%s",a);
for (int i = ; i < n;i++) _max = max(_max, a[i]);
bfs();
ans[n] = '\0';
printf("Case #%d: %s\n",cs,ans);
}
return ;
}

hdu 6223 Infinite Fraction Path的更多相关文章

  1. HDU - 6223 Infinite Fraction Path (倍增+后缀数组)

    题意:给定一个长度为n(n<=150000)的字符串,每个下标i与(i*i+1)%n连边,求从任意下标出发走n步能走出的字典序最大的字符串. 把下标看成结点,由于每个结点有唯一的后继,因此形成的 ...

  2. hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)

    题目传送门 题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会 ...

  3. 2017 ACM/ICPC 沈阳 G题 Infinite Fraction Path

    The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and s ...

  4. HDU6223 Infinite Fraction Path bfs+剪枝

    Infinite Fraction Path 这个题第一次看见的时候,题意没搞懂就没做,这第二次也不会呀.. 题意:第i个城市到第(i*i+1)%n个城市,每个城市有个权值,从一个城市出发走N个城市, ...

  5. 2017沈阳区域赛Infinite Fraction Path(BFS + 剪枝)

    Infinite Fraction Path Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java ...

  6. HDU6223——2017ICPC沈阳G Infinite Fraction Path

    题意: 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出路径上,字典序最大的,长度为n的串. 参考:https://www.cnblogs.com/mountaink/p/954144 ...

  7. ACM-ICPC 2017 沈阳赛区现场赛 G. Infinite Fraction Path && HDU 6223(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6223 参考题解:https://blog.csdn.net/qq_40482495/article/d ...

  8. Infinite Fraction Path HDU 6223 2017沈阳区域赛G题题解

    题意:给你一个字符串s,找到满足条件(s[i]的下一个字符是s[(i*i+1)%n])的最大字典序的长度为n的串. 思路:类似后缀数组,每次倍增来对以i开头的字符串排序,复杂度O(nlogn).代码很 ...

  9. Infinite Fraction Path(HDU6223 + bfs + 剪枝)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6223 题目: 题意: 给你一个长度为n的数字串,开始时你选择一个位置(记为i,下标从0开始)做为起点 ...

随机推荐

  1. 算法-----数组------ 数组中的第K个最大元素

    在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 ...

  2. samba与apache配置使用

    samba与apache根目录 1.直接将apache用户作为samba用户 2.给apache用户赋宇网站根目录的acl rwx权限 #注意 第一次不要加默认的权限 setfacl -m u:apa ...

  3. GreenMail邮件测试服务器

    GreenMail邮件测试服务器 http://blog.csdn.net/jackiehff/article/details/8741988 这个目前没有需求,所以暂不研究

  4. 如何修改Github上提交的错误用户地址和姓名

    Changing author info  https://help.github.com/articles/changing-author-info/   To change the name an ...

  5. Linux-ls,cd,type命令

    windows: dll:dynamic link library,动态链接库 Linux: .so:shared object,共享对象 操作系统: kernel:内核: 1.进程管理 2.内核管理 ...

  6. git安装后Gitbase闪退,gui无法使用问题解决

    一般是因为null.sys导致,根本原因应该还是你装的盗版系统有问题,解决办法如下 cmd 打开命题提示符后  输入  sc  start null  看 null.sys是否有问题,如果有问题,重新 ...

  7. mysql修改外部访问权限

    mysql>use mysql; mysql>update user set host =’%’ where user=’root’ mysql>select host,user f ...

  8. TW实习日记:第15天

    今天又是修修补补的一天,不过最开心的是因为项目比较特殊,有自己的后端服务器,有一些接口相关的bug可以让我直接写Java代码,终于可以碰一碰Java了哈哈.有好几个bug都是之前的人粗心设置了多余或者 ...

  9. FFT的物理意义(转载)

    文章转载自: http://blog.sina.com.cn/s/blog_640029b301010xkv.html FFT是离散傅立叶变换的快速算法,可以将一个信号变换到频域.有些信号在时域上是很 ...

  10. [leetcode-648-Replace Words]

    In English, we have a concept called root, which can be followed by some other words to form another ...