Codeforces Round #179 (Div. 2) B.Yaroslav and Two Strings(容斥原理)

题目链接

比赛的时候题给看错了。。当作神题了,看了一下题解,思考了好一会,终于在虎哥的帮助下,明白了。

求出所有的位置上s1[i]>=s2[i]的情况数,s1[i] <= s2[i]的情况数,小小的容斥一下,即可。中间各种错误,各种WA。

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <iostream>

 4 using namespace std;

 5 #define LL __int64

 6 #define MOD 1000000007

 7 char s1[100001],s2[100001];

 8 int sum[100001],p1[100001],p2[100001],p3[100001];

 9 LL a,b,c,d;

10 int main()

11 {

12     int n,i;

13     scanf("%d%s%s",&n,s1,s2);

14     for(i = 0;i < n;i ++)

15     {

16         if(s1[i] == '?'&&s2[i] == '?')

17         {

18             sum[i] = 100;

19             p1[i] = 55;

20             p2[i] = 55;

21             p3[i] = 10;

22         }

23         else if(s1[i] == '?')

24         {

25             sum[i] = 10;

26             p1[i] = 10 - (s2[i]-'0');

27             p2[i] = s2[i] - '0' + 1;

28             p3[i] = 1;

29         }

30         else if(s2[i] == '?')

31         {

32             sum[i] = 10;

33             p1[i] = s1[i] - '0'+1;

34             p2[i] = 10 - (s1[i]-'0');

35             p3[i] = 1;

36         }

37         else

38         {

39             if(s1[i] >= s2[i])

40             p1[i] = 1;

41             if(s1[i] <= s2[i])

42             p2[i] = 1;

43             if(s1[i] == s2[i])

44             p3[i] = 1;

45             sum[i] = 1;

46         }

47     }

48     a = b = c = d = 1;

49     for(i = 0;i < n;i ++)

50     {

51         a = ((LL)(a*sum[i]))%MOD;

52         b = ((LL)(b*p1[i]))%MOD;

53         c = ((LL)(c*p2[i]))%MOD;

54         d = ((LL)(d*p3[i]))%MOD;

55     }

56     a = (a - b - c + d)%MOD;

57     if(a < 0)

58     a += MOD;

59     printf("%I64d\n",a);

60     return 0;

61 }

 

你可能感兴趣的:(codeforces)