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 = 150000+10;
const int MOD = 1e10+7;
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 = 0, int step = 0) :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, 0, sizeof(n));
    t = 0;
    memset(vis, 0, sizeof(vis));
    for (int i = 0; i < n; i++)ans[i] = ‘0‘-1;
    _max = ‘0‘-1;
}

void bfs() {
    priority_queue<Node>que;
    for (int i = 0; i < n; i++) {
        if (_max == a[i]) { que.push(Node(i, 0));  }//初始将值最大的元素节点压入队列
    }
    int pre_step = -1,t=0;
    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+1)%n,p.step+1));
    }

}

int main() {
    int t; scanf("%d",&t);
    for (int cs = 1; cs <= t;cs++) {
        scanf("%d", &n);
        init(n);
        scanf("%s",a);
        for (int i = 0; i < n;i++) _max = max(_max, a[i]);
        bfs();
        ans[n] = ‘\0‘;
        printf("Case #%d: %s\n",cs,ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/ZefengYao/p/9775648.html

时间: 2024-08-26 15:28:30

hdu 6223 Infinite Fraction Path的相关文章

HDU 6223 Infinite Fraction Path(BFS+剪枝)

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

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

题目传送门 题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会得到一个长度为n的数列,输出能得到的最大的数列(当成数字). 思路: 一个数字肯定是最高位越大,这个数字本身就越大,所以肯定第一位要取最大值,在这一位取最大值的时候后面每一位都要尽量最大,所以想到bfs. 但是bfs肯定要剪枝,怎么剪枝呢? 1.按照思路,我要取每一位尽可能大的值,所以某一个状态的某

HDU6223——2017ICPC徐州G Infinite Fraction Path

题意: 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出路径上,字典序最大的,长度为n的串. 参考:https://www.cnblogs.com/mountaink/p/9541442.html 思路: BFS, 一个数字肯定是最高位越大,这个数字本身就越大,所以肯定第一位要取最大值,在这一位取最大值的时候后面每一位都要尽量最大,所以想到bfs. 但是bfs肯定要剪枝,怎么剪枝呢? 1.按照思路,我要取每一位尽可能大的值,所以某一个状态的某一位小于我当前以及有的解,这个状态肯定

ACM-ICPC 2017 沈阳赛区现场赛 G. Infinite Fraction Path &amp;&amp; HDU 6223

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6223 参考题解:https://blog.csdn.net/qq_40482495/article/details/78492841 注意优先队列自定义比较级的用法!! 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define ull unsigned long long 5 #define

【HDOJ6223】Infinite Fraction Path(后缀数组)

题意: 给一个长度为n的字符串s[0..n-1],但i的后继不再是i+1,而是(i*i+1)%n,求所有长度为n的"子串"中,字典序最大的是谁 n<=150000,s[i]=0..9 思路:后缀数组 因为前驱与后继的关系已经变化,就不能用下标直接加减 i的后继是唯一的,i的前驱却不一定 所以对于后继使用倍增,对于前驱每个位置暴力开队列存储,需要的时候再拿出来 在判断的地方稍作修改 1 #include<cstdio> 2 #include<cstring>

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

题意 给定一个数字串,每个位子都能向(i*i+1)%n的位子转移,输出在路径上.字典序最大的.长度为n的串($n \leq 150000$). 分析 先考虑一个暴力的方法,考虑暴力每个x,然后O(n)判定形成的字符串字典序是否比当前的最优解要大,复杂度O(n²),显然大家都会做. 而本题中有个结论:没有必要每次O(n),只要前100个字符一样,那么后面的一定都一样!所以>500直接break,复杂度O(500n), 可以过! 理解:对于所有的下标k,k向(k*k+1)%n连一条有向边,最后可以得

HDU - 2290 Find the Path(最短路)

HDU - 2290 Find the Path Time Limit: 5000MS   Memory Limit: 64768KB   64bit IO Format: %I64d & %I64u Submit Status Description Scofield is a hero in American show "Prison Break". He had broken the prison and started a big runaway. Scofield h

hdu 4775 Infinite Go(暴力)

题目链接:hdu 4775 Infinite Go 题目大意:两个人下围棋,总共走了n步,黑棋和白棋交替走,如果一片棋的上下左右被封死,那么该片棋子就会被吃掉,问说最后黑白棋各剩多少个. 解题思路:比较恶心的模拟题,相邻相同色的棋子要用并查集连接,并且要记录每片棋子还剩的空格数,如果空格数为0的话说明该片棋子被其他颜色围住,则要剔除掉,不且将相邻的位置不同色的棋空格数加1.主要是细节上的问题. 样例 8 7 5 5 4 5 3 5 3 4 4 4 3 3 4 6 18 1 3 1 4 2 2 1

HDU 4775 Infinite Go(并查集,模拟)

HDU 4775 Infinite Go 题目链接 题意:围棋,两人轮流走,如果有一链被围死,就会被吃掉,问下完后最后黑色和白色各剩多少棋 思路:模拟,利用一个并查集来保存链,然后并记录下周围有多少个空格,然后去模拟,注意几个点,就是删除的时候,要把空格还回去,还有边界的位置是也算被围死的 代码: #include <stdio.h> #include <string.h> #include <queue> #include <map> using name