You Are Given a Decimal String..(思维最短路)

You Are Given a Decimal String…

题目
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:

it prints 0, and adds 4 to its value, so the current value is 4, and the output is 0;
it prints 4, and adds 4 to its value, so the current value is 8, and the output is 04;
it prints 8, and adds 4 to its value, so the current value is 12, and the output is 048;
it prints 2, and adds 2 to its value, so the current value is 14, and the output is 0482;
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 areallowed.

Input
The first line contains a single string s (1≤|s|≤2⋅10^6, 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.

题意
大概讲一下题意,就是给定的字符串是机器打印出来的子序列,而这个子序列是计算器上每次计算后的个位数,求0~9中第i行第j列表示i-j计算器,只能加i或者加j,求出现这个子序列最少需要插入多少个数?(或者理解为最少要用多少次计算器)

思路
思路的话,最少次数可用最短路的思想来思考,即用计算器计算从x到y的最短路,详细见代码。

代码

#include<bits/stdc++.h>
#define ll long long
#define R register int
#define inf 0x3f3f3f3f
#define mod 1000000007;
using namespace std;
inline ll read(){
   ll s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
   return s*w;
}

void put1(){ puts("Yes") ;}
void put2(){ puts("No") ;}

const int manx=2e6+5;
ll d[10][10];
string s;

ll f(ll x, ll y)
{
    memset(d,inf,sizeof(d));  // 距离数组初始化
 // x-y计算器可以直接到达的点 例如1-2计算器 可以直接从1到2 从1到3 等等
    for(int i=0;i<10;i++)   
        d[i][(i+x)%10]=d[i][(i+y)%10]=1;  
    for(int k=0;k<10;k++)
        for(int i=0;i<10;i++)
            for(int j=0;j<10;j++) //最短路 算从i到j的最少次数
                d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
    ll ans=0;
    for(int i=0;i<s.size()-1;i++)
    {
        int a=s[i]-'0',b=s[i+1]-'0';
        if(d[a][b]>999999) return -1; //如果这个数无法到达 
        else ans+=d[a][b]-1;  //为什么-1?想一想 例如0-1计算器从0到8(可以看成1到9) 中间只有2~8 7个数 即最少插入d[a][b]-1个数
    }
    return ans;
}

int main()
{

    cin>>s;
    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++)
            cout<<f(i,j)<<" ";
        cout<<endl;
    }
    return 0;
}
发布了50 篇原创文章 · 获赞 15 · 访问量 4243

猜你喜欢

转载自blog.csdn.net/JiangHxin/article/details/103113162