POJ 3340 & HDU 2410 Barbara Bennett's Wild Numbers(數學)

來源:互聯網
上載者:User

標籤:poj   hdu   數學   

題目連結:

PKU:http://poj.org/problem?id=3340

HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410


Description

A wild number is a string containing digits and question marks (like 36?1?8). A number X matches a wild number W if they have the same length, and every non-question mark character in X is equal to the character in the same position in W (it means that you can replace a question mark with any digit). For example, 365198 matches the wild number 36?1?8, but 360199, 361028, or 36128 does not. Write a program that reads a wild number W and a number X from input, both of length n, and determines the number of n-digit numbers that match W and are greater than X.

Input

There are multiple test cases in the input. Each test case consists of two lines of the same length. The first line contains a wild number W, and the second line contains an integer number X. The length of input lines is between 1 and 10 characters. The last line of input contains a single character #.

Output

For each test case, write a single line containing the number of n-digit numbers matching W and greater than X (n is the length of W and X).

Sample Input

36?1?82364288?3910?5#

Sample Output

10004

Source

Tehran 2006


題意:

給出兩個數字字串,串一中有‘?‘,問在‘?’位置填數字共有多少中填法,保證串一大於串二!


代碼如下:

#include <cstdio>#include <cstring>#include <cmath>const int maxn = 17;typedef __int64 LL;int cont;LL ans;char s1[maxn];char s2[maxn];LL POW(LL n, LL k){    LL s = 1;    for(LL i = 0; i < k; i++)    {        s*=n;    }    return s;}void solve(int len, int k){    for(int i = 0; i < len; i++)    {        if(s1[i] == '?')        {            ans+=(9-(s2[i]-'0'))*POW(10,--cont);        }        else if(s1[i] < s2[i])            return ;        else if(s1[i] > s2[i])        {            ans+=POW(10,cont);            return ;        }    }}int main(){    while(~scanf("%s",s1))    {        ans = 0;        cont = 0;        if(s1[0] == '#')            break;        scanf("%s",s2);        int len = strlen(s1);        for(int i = 0; i < len; i++)        {            if(s1[i] == '?')                cont++;        }        solve(len,0);        printf("%I64d\n",ans);    }    return 0;}


POJ 3340 & HDU 2410 Barbara Bennett's Wild Numbers(數學)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.