Infinite Fraction Path

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 5756    Accepted Submission(s): 1142

Problem Description
The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and solicited an audience with the king. He recounted how he had built a happy path in the kingdom of happiness. The king affirmed Welly’s talent and hoped that this talent can help him find the best infinite fraction path before the anniversary.
The kingdom has N cities numbered from 0 to N - 1 and you are given an array D[0 ... N - 1] of decimal digits (0 ≤ D[i] ≤ 9, D[i] is an integer). The destination of the only one-way road start from the i-th city is the city labelled (i2 + 1)%N.
A path beginning from the i-th city would pass through the cities u1,u2,u3, and so on consecutively. The path constructs a real number A[i], called the relevant fraction such that the integer part of it is equal to zero and its fractional part is an infinite decimal fraction with digits D[i], D[u1], D[u2], and so on.
The best infinite fraction path is the one with the largest relevant fraction
 
Input
The input contains multiple test cases and the first line provides an integer up to 100 indicating to the total numberof test cases.
For each test case, the first line contains the integer N (1 ≤ N ≤ 150000). The second line contains an array ofdigits D, given without spaces.
The summation of N is smaller than 2000000.
 
Output
For each test case, you should output the label of the case first. Then you are to output exactly N characters which are the first N digits of the fractional part of the largest relevant fraction.
 
Sample Input
4
3
149
5
12345
7
3214567
9
261025520
 
Sample Output
Case #1: 999
Case #2: 53123
Case #3: 7166666
Case #4: 615015015
 
Source
 
Recommend
jiangzijing2015   |   We have carefully selected several similar problems for you:  6730 6729 6728 6727 6726 
 
这题要注意的细节还是有点多的,算是比较细腻处理的bfs的题目了.
  1. /*************************************************************************
  2. > File Name: hdu-6223.infinite_fraction_path.cpp
  3. > Author: CruelKing
  4. > Mail: 2016586625@qq.com
  5. > Created Time: 2019年09月18日 星期三 15时57分37秒
  6. 本题思路:BFS暴力,先找出最大的几个数的位置,接着bfs每次寻找下一层最大的值,直到
  7. 找到答案为止.
  8. 注意剪枝:1.如果在当前层寻找到了比最大值小的值直接pop.
  9. 2.如果当前层有多个结点通往下一层的同一个结点,只需要保留一个就行了.
  10. ************************************************************************/
  11.  
  12. #include <cstdio>
  13. #include <queue>
  14. #include <cstring>
  15. using namespace std;
  16.  
  17. typedef long long ll;
  18.  
  19. const int maxn = + ;
  20. char str[maxn], ans[maxn];
  21.  
  22. int M[maxn], tot;
  23. bool vis[maxn];
  24.  
  25. int n;
  26.  
  27. ll sta[maxn], top;
  28.  
  29. struct node {
  30. ll index, step;
  31. };
  32.  
  33. bool operator < (node a, node b) {
  34. if(a.step == b.step) return str[a.index] < str[b.index];
  35. return a.step > b.step;
  36. }
  37.  
  38. void bfs() {
  39. priority_queue <node> que;
  40. for(int i = ; i < tot; i ++) {
  41. que.push((node) {M[i], });//最大值入队列
  42. }
  43. ll last = ;
  44. while(!que.empty()) {
  45. node now = que.top();
  46. que.pop();
  47. if(last != now.step) {
  48. last = now.step;
  49. while(top) vis[sta[-- top]] = false;//把当前标记过得结点都释放,因为他们还可以继续访问
  50. }
  51. if(ans[now.step] > str[now.index] || now.step >= n || vis[now.index]) continue;//如果在当前层当前位置已经被访问过了就跳过这个结点, 如果当前已经找到了n个数就跳过这个结点,如果已经当前结点字典序小于之前访问过的最大值就跳过这个结点
  52. sta[top ++] = now.index;//把当前访问的结点放入队列并标记
  53. vis[now.index] = true;
  54. ans[now.step] = str[now.index];//更新答案
  55. que.push((node) {(now.index * now.index + ) % n, now.step + });//跳跃
  56. }
  57. while(top) vis[sta[-- top]] = false;
  58. ans[n] = '\0';
  59. }
  60.  
  61. int _case;
  62.  
  63. void print() {
  64. printf("Case #%d: %s\n", ++_case, ans);
  65. }
  66.  
  67. int main() {
  68. int t, _case = ;
  69. int Max;
  70. char k;
  71. scanf("%d", &t);
  72. while(t --) {
  73. for(int i = ; i < n; i ++) ans[i] = ;
  74. k = ;
  75. tot = ;
  76. scanf("%d", &n);
  77. scanf("%s", str);
  78. for(int i = ; i < n; i ++) {
  79. if(str[i] > k) {
  80. k = str[i];
  81. }
  82. }
  83. for(int i = ; i < n; i ++) {
  84. if(str[i] == k) {
  85. M[tot ++] = i;//存储字符串中的最大值
  86. }
  87. }
  88. bfs();
  89. print();
  90. }
  91. return ;
  92. }

2017沈阳区域赛Infinite Fraction Path(BFS + 剪枝)的更多相关文章

  1. HDU6223 Infinite Fraction Path bfs+剪枝

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

  2. HDU6223 && 2017沈阳ICPC: G. Infinite Fraction Path——特殊图&&暴力

    题意 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出在路径上.字典序最大的.长度为n的串($n \leq 150000$). 分析 先考虑一个暴力的方法,考虑暴力每个x,然后O(n) ...

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

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

  4. 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 ...

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

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

  6. hdu6229 Wandering Robots 2017沈阳区域赛M题 思维加map

    题目传送门 题目大意: 给出一张n*n的图,机器人在一秒钟内任一格子上都可以有五种操作,上下左右或者停顿,(不能出边界,不能碰到障碍物).题目给出k个障碍物,但保证没有障碍物的地方是强联通的,问经过无 ...

  7. HDU 6229 Wandering Robots(2017 沈阳区域赛 M题,结论)

    题目链接  HDU 6229 题意 在一个$N * N$的格子矩阵里,有一个机器人. 格子按照行和列标号,左上角的坐标为$(0, 0)$,右下角的坐标为$(N - 1, N - 1)$ 有一个机器人, ...

  8. Heron and His Triangle 2017 沈阳区域赛

    A triangle is a Heron’s triangle if it satisfies that the side lengths of it are consecutive integer ...

  9. 2017 ICPC/ACM 沈阳区域赛HDU6223

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

随机推荐

  1. 《Head First 软件开发》阅读三

    足够好的设计:以良好的设计完成工作 良好的设计有助于交付软件. 有些项目的进程会打破单一责任的原则,当每个对象只有一个理由去改变时,已经正确地实施了单一责任原则.辨别设计中的多重责任,对实现类中的东西 ...

  2. 【NOIP2016提高A组模拟8.23】函数

    题目 分析 观察这个是式子\(\sum_{d|n}f(n)=n\), 发现其实函数\(f()\)就是欧拉函数\(φ()\)(见http://blog.csdn.net/chen1352/article ...

  3. Redis :Linux和Window查看版本号

    一.Linux版本:查看服务端版本 **二者都可以** redis-server -v redis-server --version 查看客户端版本 **二者都可以** redis-cli -v re ...

  4. C++ 没有合适的默认构造函数(无参数构造函数)

    本来今天吧,想写一个proxy class的范例,写着写着出了个问题,见如下代码 ; Array1D* _elemArray = new Array1D[_cap]; 同时我为Array1D这个类写了 ...

  5. 引用自定义的css或者js文件

    用script标签,src是js文件路径 <script type="text/javascript" src="./js/udc.js">< ...

  6. js中[]、{}、()区别

    一.{ } 大括号,表示定义一个对象,大部分情况下要有成对的属性和值,或是函数体 {}表示对象.[]表示对象的属性.方法,()如果用在方法名后面,代表调用 如:var LangShen = {&quo ...

  7. [NOIP2017]注意点

    1.数据大却没开long long 导致的gg.2.文件读入时stdin打成stdout...3.桶维护数值,有负值要平移,且数值最好稍大(否则可能RE).4.很智障地打错变量.5.DP或其他涉及到转 ...

  8. KafKa集群安装详细步骤

    最近在使用Spring Cloud进行分布式微服务搭建,顺便对集成KafKa的方案做了一些总结,今天详细介绍一下KafKa集群安装过程: 1. 在根目录创建kafka文件夹(service1.serv ...

  9. centos7 修改ali yum源

    centos7 修改yum源为阿里源,某下网络下速度比较快 首先是到yum源设置文件夹里 安装base reop源 cd /etc/yum.repos.d 接着备份旧的配置文件 sudo mv Cen ...

  10. leetcode-mid-math-172. Factorial Trailing Zeroes-NO-????

    mycode 问题:为甚在小于200的时候,答案ok,大于等于200的时候,就少一个1??? class Solution(object): def trailingZeroes(self, n): ...