2019湖南省赛 Numbers(dfs)

Numbers
Bobo has n n distinct integers a 1 , a 2 , , a n a_1, a_2, \dots, a_n in [ 0 , 99 ] [0, 99] . He writes them in decimal notation without leading zeros in a row, obtaning a string s s .

Given the string s s , find the number of possible array of integers a 1 , a 2 , , a n a_1, a_2, \dots, a_n .

Input
The input consists of several test cases and is terminated by end-of-file.

Each test case contains a string s s .

1 s 50 1 \leq |s| \leq 50
There are at most 100 100 test cases.
Output
For each test case, print an integer which denotes the result.

思路:
搜一下就好了

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>

using namespace std;

char s[105];
int vis[105],len;

int dfs(int i)
{
    if(i == len + 1)
    {
        return 1;
    }
    
    int ans = 0;
    int x = s[i] - '0';
    
    if(!vis[x])
    {
        vis[x] = 1;
        ans += dfs(i + 1);
        vis[x] = 0;
    }
    
    if(i != len && x != 0)
    {
        int y = s[i + 1] - '0';
        int num = x * 10 + y;
        if(!vis[num])
        {
            vis[num] = 1;
            ans += dfs(i + 2);
            vis[num] = 0;
        }
    }
    
    return ans;
}

int main()
{
    while(~scanf("%s",s + 1))
    {
        memset(vis,0,sizeof(vis));
        len = (int)strlen(s + 1);
        printf("%d\n",dfs(1));
    }
    return 0;
}

发布了756 篇原创文章 · 获赞 27 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104642173