惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
Help Net Security
Help Net Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
博客园_首页
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LINUX DO - 最新话题
PCI Perspectives
PCI Perspectives
博客园 - 三生石上(FineUI控件)
V2EX - 技术
V2EX - 技术
Spread Privacy
Spread Privacy
T
Tor Project blog
量子位
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
小众软件
小众软件
博客园 - 叶小钗
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Blog — PlanetScale
Blog — PlanetScale
H
Help Net Security
Y
Y Combinator Blog
N
News | PayPal Newsroom
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Tenable Blog
Scott Helme
Scott Helme
G
GRAHAM CLULEY
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
IT之家
IT之家
Schneier on Security
Schneier on Security
F
Fortinet All Blogs
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
博客园 - 司徒正美
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Last Watchdog
The Last Watchdog
L
LangChain Blog
C
Check Point Blog
Google Online Security Blog
Google Online Security Blog
V
Visual Studio Blog
Latest news
Latest news

博客园 - ZefengYao

Markdown 入门与 Word 使用指南 关于崩溃报告的日志以及dump文件 2017南宁现场赛E The Champion ACM-ICPC 2018 南京赛区网络预赛 Sum c语言几个字符串处理函数的简单实现 各种类型排序的实现及比较 随机洗牌算法Knuth Shuffle和错排公式 两个栈实现队列 面试杂题 面试题——栈的压入、弹出顺序 C++ 智能指针的简单实现 openGL初学函数解释汇总 foj Problem 2107 Hua Rong Dao foj Problem 2282 Wand UVA-1400 Ray, Pass me the dishes! 《挑战程序设计竞赛》 利用后缀数组求最长回文串 Uva 11174 Stand in a Line UVA 11375 Matches poj 3729 Facer’s string
hdu 6223 Infinite Fraction Path
ZefengYao · 2018-10-11 · via 博客园 - ZefengYao

题目地址: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;
}