[Blue Bridge Cup][Algorithm to improve VIP] Generated number

Time Limit: 1Sec Memory Limit: 128MB Commits: 49 Resolved: 15

Topic description
Given an integer n (n< 10^30) and k transformation rules (k<=15). 

Rule: 

One digit can be transformed into another one digit: 

The right part of the rule cannot be zero. 

For example: n=234. There are rules (k=2): 

2-> 5 

3-> 6 

The above integers 234 The integers that may be generated after transformation are (including the original number): 

234 

534 

264 

564  There

are 4 different generated numbers 

problem: 

give An integer n and k rules. 

Find: 

After any number of transformations (0 or more times), how many different integers can be generated. 

Only the number of outputs is required. 
enter
n k 
x1 y1 
x2  y2
... ... 
xn at 
output
an integer (the number that satisfies the condition)
sample input
234  2
2  5
3  6
Sample output
4


#include<cstdio>
#include<string>
#include<set>
#include<iostream>
using namespace std;
int num[11],cnt,A[31],k;
int ans[100];
string s;
set<int>se[10];
bool vis[10]={false};
void DFS(int u)
{
    if(!vis[u])
    {
        vis [u] = true ;
        cnt++;
        for(set<int>::iterator it=se[u].begin();it!=se[u].end();it++)
            DFS(*it);
    }
}
int main(void)
{
    cin>>s>>k;
    for(int i=1;i<=k;i++)
    {
        int u, v;
        cin>>u>>v;
        se[u].insert(v);
    }
    A[0]=s.length();
    for(int i=1;i<=A[0];i++) A[i]=s[i-1]-48;
    for(int i=0;i<=9;i++)
    {
        cnt=0;
        fill (vis, vis + 10 , 0 );
        DFS(i);
        num[i] = cnt;
    }
    ans[0]=1,ans[1]=1;     
    for(int i=1;i<=A[0];i++)
    {
        for(int j=1;j<=ans[0];j++) ans[j]*=num[A[i]];
        for(int j=1;j<=ans[0];j++)
            if(ans[j]>=10)
            {
                for(int k=1;k<=ans[0];k++)
                {
                    if (ans[ans[ 0 ]]>= 10 ) ans[ 0 ]++ ;
                    years[k + 1 ]+=years[k]/ 10 ;
                    years[k] %= 10 ;
                }
            }
    }
    for(int i=ans[0];i>=1;i--) cout<<ans[i]; 
    //cout<<ans;
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324936222&siteId=291194637