zoukankan      html  css  js  c++  java
  • [CF1202B] You Are Given a Decimal String(最短路)

    Description

    今天突然想来发一篇博客防死

    [Portal][https://vjudge.net/problem/2650668/origin]

    定义被x-y生成器生成的序列为, 一开始有一个数字S = 0, 每次输出S % 10, 然后把这个数字加上x或y.

    现在给你一个串, 对于$0leq x, y leq 9 $要你计算至少要在串中插入几个数位才能将其变成正确的x - y生成器生成的串

    字符串长度(leq 2e6)

    Solution

    这种数字之间跳来跳去的直接考虑最短路.

    (i)((i + x) mod 10), 连一条边权为1的单向边.

    (i)((i + y) mod 10), 连一条边权为1的单向边.

    (dis[i][i] = inf)

    直接跑floyd即可, 那么答案就是数字之间的距离-1的和.

    Code

    #include<bits/stdc++.h>
    using namespace std;
    #define rep(i, a, b) for(int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
    #define drep(i, a, b) for(int i = (a), i##_end_ = (b); i >= i##_end_; --i)
    #define clar(a, b) memset((a), (b), sizeof(a))
    #define debug(...) fprintf(stderr, __VA_ARGS__)
    typedef long long LL;
    typedef long double LD;
    const double pi = acos(-1);
    const int BUF_SIZE = (int)1e6 + 10;
    template <typename T> inline bool chkmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
    template <typename T> inline bool chkmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
    LL read() {
    	char ch = getchar();
    	LL x = 0, flag = 1;
    	for (;!isdigit(ch); ch = getchar()) if (ch == '-') flag *= -1;
    	for (;isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
    	return x * flag;
    }
    
    const int Maxn = 2e6 + 9;
    char s[Maxn];
    
    void Init() {
    	scanf("%s", s + 1);
    }
    
    int dis[10][10];
    int calc(int x, int y) {
    	clar(dis, 0x3f);
    	rep (i, 0, 9) dis[i][(i + x) % 10] = 1, dis[i][(i + y) % 10] = 1;
    
    	rep (k, 0, 9)
    		rep (i, 0, 9)
    			rep (j, 0, 9) dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
    
    	int ans = 0;
    	rep (i, 1, strlen(s + 1) - 1) {
    		if (dis[s[i] - '0'][s[i + 1] - '0'] == 0x3f3f3f3f) return -1;
    		ans += max(dis[s[i] - '0'][s[i + 1] - '0'] - 1, 0);
    	}
    	return ans;
    }
    
    void Solve() {
    	rep (i, 0, 9) {
    		rep (j, 0, 9) printf("%d ", calc(i, j));
    		puts("");
    	}
    }
    
    int main() {
    //	freopen("bosky.in", "r", stdin);
    //	freopen("bosky.out", "w", stdout);
    
    	Init();
    	Solve();
    
    #ifdef Qrsikno
    	debug("
    Running time: %.3lf(s)
    ", clock() * 1.0 / CLOCKS_PER_SEC);
    #endif
    	return 0;
    }
    
  • 相关阅读:
    Solution 16: 树的层次遍历
    Solution 15: 树的镜像
    Solution 14: Two Sum
    Solution 13: 链表的倒数第K个节点
    Solution 10: 翻转句子中的单词
    Solution 11: 二叉树中节点的最大距离
    Solution 9: 判断序列是否为BST的后续遍历结果
    Solution 7: 判断两链表是否相交
    估算Baidu和Google的网页索引数量之比
    主元素问题
  • 原文地址:https://www.cnblogs.com/qrsikno/p/11365961.html
Copyright © 2011-2022 走看看