hdu 6223 Infinite Fraction Path
题目地址: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的更多相关文章
- HDU - 6223 Infinite Fraction Path (倍增+后缀数组)
题意:给定一个长度为n(n<=150000)的字符串,每个下标i与(i*i+1)%n连边,求从任意下标出发走n步能走出的字典序最大的字符串. 把下标看成结点,由于每个结点有唯一的后继,因此形成的 ...
- hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)
题目传送门 题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会 ...
- 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 ...
- HDU6223 Infinite Fraction Path bfs+剪枝
Infinite Fraction Path 这个题第一次看见的时候,题意没搞懂就没做,这第二次也不会呀.. 题意:第i个城市到第(i*i+1)%n个城市,每个城市有个权值,从一个城市出发走N个城市, ...
- 2017沈阳区域赛Infinite Fraction Path(BFS + 剪枝)
Infinite Fraction Path Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java ...
- HDU6223——2017ICPC沈阳G Infinite Fraction Path
题意: 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出路径上,字典序最大的,长度为n的串. 参考:https://www.cnblogs.com/mountaink/p/954144 ...
- 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 ...
- Infinite Fraction Path HDU 6223 2017沈阳区域赛G题题解
题意:给你一个字符串s,找到满足条件(s[i]的下一个字符是s[(i*i+1)%n])的最大字典序的长度为n的串. 思路:类似后缀数组,每次倍增来对以i开头的字符串排序,复杂度O(nlogn).代码很 ...
- Infinite Fraction Path(HDU6223 + bfs + 剪枝)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6223 题目: 题意: 给你一个长度为n的数字串,开始时你选择一个位置(记为i,下标从0开始)做为起点 ...
随机推荐
- 【Consul】Consul架构-Consensus协议
Consul使用Consensus协议提供一致性(Consistency)--CAP定义的一致性.Consensus协议是基于"Raft:In search of an Understand ...
- LeetCode:24. Swap Nodes in Pairs(Medium)
1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...
- 获取单片机唯一id(stm32获取单片机唯一id)
stm32唯一id: 不同型号的stm32单片机,id不在同一地址上!具体地址可以通过用户手册中的Device electronic signature>Unique device ID reg ...
- 快速平方根算法的javascript实现
前几天看见了一个来自雷神之槌的平方根源码,原理多方有介绍,不赘述. 源码是c语言写的,我思考后发现这样的算法在javascript中也是可以完成的. function InvSqrt(x){ var ...
- JMeter学习笔记(九) 参数化3--User Defined Variables
3.User Defined Variables 1)添加用户定义的变量 2)添加变量 3)添加HTTP请求,引用变量,格式:${} 4)执行HTTP请求,察看结果树 5)用户定义的变量,优缺点: * ...
- Python学习笔记(二)一一一字典总结
创建方式:1 直接创建 newDictonary={‘key’:'value',} 2 列表转字典(dict函数) 3 基本操作:len 返回总数 dictionary[k] 返回k对应的值 ...
- 如何用Fiddler 拦住RestAssured发出的请求
用RestAssured 发出的请求并不能直接被fiddler 拦截,可以在初始化的时候做出如下配置: RestAssured.proxy("localhost", 8888); ...
- Captcha 验证码Example
maven依赖 防止和spring中的servlet冲突 <dependency> <groupId>com.github.penggle</groupId> &l ...
- Julia 学习笔记(一):数组
个人向,只会记录一些需要注意的点. 前言 学习 Julia 已经有一段时间了,但是进步缓慢.这一方面是最近代码写得少,一方面是 Julia 学习资料少.中文资料更少,但也有我没做笔记的缘故导致学习效率 ...
- JavaScript Map数据结构
Array.prototype.remove = function (s) { for (var i = 0; i < this.length; i++) { if (s == this[i]) ...