题目地址: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. 类的__new__方法使用

    class Person(object): def __init__(self): self.name ="aaa" def defineName(self): self.name ...

  2. VI的配置

    vi下设置tab键为4个空格 在每个用户的主目录下,都有一个 vi 的配置文件".vimrc"或".exrc",没有的可以新建一个.用户可以编辑它,使这些设置在 ...

  3. 使用USB Key(加密狗)实现身份认证

    首先你需要去买一个加密狗设备,加密狗是外形酷似U盘的一种硬件设备! 这里我使用的坚石诚信公司的ET99产品 公司项目需要实现一个功能,就是客户使用加密狗登录, 客户不想输入任何密码之类的东西,只需要插 ...

  4. linux开发基本库

    1.ZeroMQ zmq是一个消息队列.可以在进程内.进程间.TCP.多播中,以消息为单位传输数据,而不是socket的字节流.官方主页上有下载.使用.文档,蛮全的. 常用模式有:Request-Re ...

  5. Sql面试题之四(难度:中等 | 含答案 | 有逻辑题)

    Sql面试题之四(难度:中等 | 含答案 | 有逻辑题)

  6. Web负载均衡技术

    Web负载均衡(Load Balancing),简单地说就是给我们的服务器集群分配“工作任务”,而采用恰当的分配方式,对于保护处于后端的Web服务器来说,非常重要. 负载均衡的策略有很多,我们从简单的 ...

  7. 九度OJ--Q1473

    import java.util.ArrayList;import java.util.Scanner; /* * 题目描述: * 大家都知道,数据在计算机里中存储是以二进制的形式存储的. * 有一天 ...

  8. 了解游戏编程与 AI

    噫语系列... 闲话 最近在重写我的一个 QQ 群机器人项目,并尝试将它改成更通用的结构,以方便在未来加入对 Wechat 和 Telegram 的支持. 在查资料的过程中,发现很多人认为一个群内多人 ...

  9. POJ 2186 Popular Cows(强联通+缩点)

    Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...

  10. 数论3——gcd&&lcm

    gcd(a, b),就是求a和b的最大公约数 lcm(a, b),就是求a和b的最小公倍数 然后有个公式 a*b = gcd * lcm     ( gcd就是gcd(a, b), ( •̀∀•́ ) ...