【CodeForces 1202B --- You Are Given a Decimal String...】DP+Floyd

【CodeForces 1202B --- You Are Given a Decimal String...】DP+Floyd

题目来源:点击进入【CodeForces 1202B — You Are Given a Decimal String…】

Description

Suppose you have a special x-y-counter. This counter can store some value as a decimal number; at first, the counter has value 0.

The counter performs the following algorithm: it prints its lowest digit and, after that, adds either x or y to its value. So all sequences this counter generates are starting from 0. For example, a 4-2-counter can act as follows:

  1. it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
  2. it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
  3. it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
  4. it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
  5. it prints 4, and adds 4 to its value, so the current value is 18, and the output is 04824.

This is only one of the possible outputs; for example, the same counter could generate 0246802468024 as the output, if we chose to add 2 during each step.

You wrote down a printed sequence from one of such x-y-counters. But the sequence was corrupted and several elements from the sequence could be erased.

Now you’d like to recover data you’ve lost, but you don’t even know the type of the counter you used. You have a decimal string s — the remaining data of the sequence.

For all 0≤x,y<10, calculate the minimum number of digits you have to insert in the string s to make it a possible output of the x-y-counter. Note that you can’t change the order of digits in string s or erase any of them; only insertions are allowed.

Input

The first line contains a single string s (1≤|s|≤2⋅106, si∈{0−9}) — the remaining data you have. It’s guaranteed that s1=0.

Output

Print a 10×10 matrix, where the j-th integer (0-indexed) on the i-th line (0-indexed too) is equal to the minimum number of digits you have to insert in the string s to make it a possible output of the i-j-counter, or −1 if there is no way to do so.

Sample Input

0840

Sample Output

-1 17 7 7 7 -1 2 17 2 7
17 17 7 5 5 5 2 7 2 7
7 7 7 4 3 7 1 7 2 5
7 5 4 7 3 3 2 5 2 3
7 5 3 3 7 7 1 7 2 7
-1 5 7 3 7 -1 2 9 2 7
2 2 1 2 1 2 2 2 0 1
17 7 7 5 7 9 2 17 2 3
2 2 2 2 2 2 0 2 2 2
7 7 5 3 7 7 1 3 2 7

解题思路

通过双重for循环分别求出需要添加多少个数才能满足x-y-counter。
确定x,y后我们可以用cost[i][j]建图表示i-j所需步数。初始化cost所有值为Inf。然后遍历所有能够直接一步得到i-j赋值为1;然后通过Floyd算法求出所有点与点之间的最短路径。
然后我们就只需遍历字符串s中每个相邻的两个数字a,b。判断cost[a][b]是否等于Inf。
如果是等于inf,那就说明当前x,y-counter无法输出s这样的残缺字符串,所以输出-1即可。
如果不等于inf,那就说明满足题意,a,b之间所需添加的最少字符为cost[a][b]-1;

AC代码:

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define endl '\n'
const int inf = 0x3f3f3f3f;
int ans[10][10],cost[10][10];

int main()
{
    SIS;
    string s;
    cin >> s;
    int len=s.size();
    for(int x=0;x<10;x++)
    {
        for(int y=0;y<10;y++)
        {
            memset(cost,inf,sizeof(cost));
            for(int i=0;i<10;i++)
                cost[i][(i+x)%10]=cost[i][(i+y)%10]=1;
            for(int i=0;i<10;i++)
                for(int j=0;j<10;j++)
                    for(int k=0;k<10;k++)
                        cost[j][k]=min(cost[j][k],cost[j][i]+cost[i][k]);
            for(int i=0;i<len-1;i++)
            {
                int a=s[i]-'0',b=s[i+1]-'0';
                if(cost[a][b]!=inf) ans[x][y]+=cost[a][b]-1;
                else { ans[x][y]=-1; break; }
            }
        }
    }
    for(int i=0;i<10;i++)
        for(int j=0;j<10;j++)
            if(j==9) cout << ans[i][j] << endl;
            else cout << ans[i][j] << ' ';
    return 0;
}
发布了412 篇原创文章 · 获赞 135 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_41879343/article/details/104101557