您的当前位置:首页正文

luogu P1037 【产生数】

来源:华佗健康网

貌似都是用佛洛依德写的,我就来个\(DFS\)搜索的方法吧。

首先通过字符串读入来读入这个数字。

然后对每一位数字进行\(DFS\),每搜索到一个数字计数器加一。最后根据分步计算原理,将每位数可扩展的数进行相乘输出即可。

另外第四、第五组数据较大好久没有写高精度写挂了好几次滑稽


Coding:

#include<bits/stdc++.h>
using namespace std;


string num;
int k,a[20],b[20],ans = 0,sum[30] = {};//ans每一位可以扩展多少个数字
bool vis[10] = {};//vis记录当前数字有没有被搜过


inline int read()//快读
{
    int x = 0;
    char ch = getchar();
    while(ch < '0' || ch > '9') ch = getchar();
    while(ch >= '0' && ch <= '9')
    {
        x = (x<<3)+(x<<1) + ch-'0';
        ch = getchar();
    }
    return x;
}

inline void mul(int a[],int b)//低精度乘高精度
{
    for(int i = 1;i <= a[0];i++)
    {
        a[i] *= b;
    }
    for(int i = 1;i <= a[0];i++)
    {
        if(a[i] >= 10)
        {
            a[i+1] += a[i]/10;
            a[i] %= 10;
            if(i == a[0]) a[0]++;
        }
    }
}

inline void dfs(int x)
{
    vis[x] = 1;//每搜到一个打个标记
    ans++;
    for(int i = 1;i <= k;i++)
    {
        if(a[i] == x && !vis[b[i]]) dfs(b[i]);//如果符合且未被搜索
    }
}


int main()
{
    cin >> num;
    k = read();
    sum[0] = 1;
    sum[1] = 1;
    for(int i = 1;i <= k;i++)
    {
        a[i] = read();
        b[i] = read();
    }
    for(int i = 0;i < num.size();i++)
    {
        dfs(num[i]-'0');
        mul(sum,ans);
        memset(vis,0,sizeof(vis));//消除影响
        ans = 0;
    }
    for(int i = sum[0];i >= 1;i--) printf("%d",sum[i]);
    putchar('\n');

    return 0;
}

转载于:https://www.cnblogs.com/Mark-X/p/11404645.html

因篇幅问题不能全部显示,请点此查看更多更全内容