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

题目链接

Description

Yaroslav thinks that two strings s and w, consisting of digits and having length n are non-comparable if there are two numbers, i andj(1 ≤ i, j ≤ n), such that si > wi and sj < wj. Here sign si represents the i-th digit of string s, similarly, wj represents the j-th digit of string w.

A string‘s template is a string that consists of digits and question marks ("?").

Yaroslav has two string templates, each of them has length n. Yaroslav wants to count the number of ways to replace all question marks by some integers in both templates, so as to make the resulting strings incomparable. Note that the obtained strings can contain leading zeroes and that distinct question marks can be replaced by distinct or the same integers.

Help Yaroslav, calculate the remainder after dividing the described number of ways by 1000000007(109 + 7).

Input

The first line contains integer n(1 ≤ n ≤ 105) — the length of both templates. The second line contains the first template — a string that consists of digits and characters "?". The string‘s length equals n. The third line contains the second template in the same format.

Output

In a single line print the remainder after dividing the answer to the problem by number 1000000007(109 + 7).

Sample Input

Input

29009

Output

1

Input

21155

Output

0

Input

5??????????

Output

993531194

题意:

对于两个数字串 S 和 W,如果存在 i 和 j 使得:S(i)>W(i) && S(j)<W(j) 那么说这两个串是不可比较的,现在给了两个长度均为 n(1≤n≤105) 的串 S 和 W,用 ‘?‘ 代表未知的字母,问,有多少种可能的情况,使得 S  和 W 不可比较?

分析:

求出所有可能的情况的数量,设为 ans

求出 S 比 W 大的情况,即:S(i)≥W(i) 的情况数量,设为 res1

求出 S 比 W 小的情况,即;S(i)≤W(i) 的情况数量,设为 res2

求出 S 和 W 相等的情况,即:S(i)==W(i) 的情况数量,设为 res3

结果应该是 ans-res1-res2+res3

给的串的所有情况 = s完全>=w的情况  +  w完全>=s的情况  -  s==w的情况  + s>w && s<w的情况。

刚开始用的是所有情况 - 完全大于 - 完全小于 - 完全等于。  这种做法不对,少减了大于和等于 或者 小与和等于混合的情况。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 #include <cmath>
  6 #include <algorithm>
  7 #define LL __int64
  8 const int maxn = 1e5 + 10;
  9 const LL mo = 1e9 + 7;
 10 using namespace std;
 11 char s[maxn], w[maxn];
 12 LL n, cnt;
 13
 14 LL cal1()
 15 {
 16     LL i, res = 1;
 17     for(i = 0; i < n; i++)
 18     {
 19         if(s[i]!=‘?‘ && w[i]!=‘?‘)
 20         {
 21             if(s[i]<w[i])
 22             {
 23                 res = 0;
 24                 break;
 25             }
 26         }
 27         else if(s[i]==‘?‘ && w[i]==‘?‘)
 28         res = (res*55)%mo;
 29         else if(s[i]==‘?‘)
 30         res = (res*(10-w[i]+48))%mo;
 31         else
 32         res = (res*(s[i]-48+1))%mo;
 33     }
 34     return res%mo;
 35 }
 36
 37 LL cal2()
 38 {
 39     LL i, res = 1;
 40     for(i = 0; i < n; i++)
 41     {
 42         if(s[i]!=‘?‘ && w[i]!=‘?‘)
 43         {
 44             if(s[i]>w[i])
 45             {
 46                 res = 0;
 47                 break;
 48             }
 49         }
 50         else if(s[i]==‘?‘ && w[i]==‘?‘)
 51         res = (res*55)%mo;
 52         else if(s[i]==‘?‘)
 53         res = (res*(w[i]-48+1))%mo;
 54         else
 55         res = (res*(10-s[i]+48))%mo;
 56     }
 57     return res%mo;
 58 }
 59
 60 LL cal3()
 61 {
 62     LL i, res = 1;
 63     for(i = 0; i < n; i++)
 64     {
 65         if(s[i]!=‘?‘ && w[i]!=‘?‘)
 66         {
 67             if(s[i]!=w[i])
 68             {
 69                 res = 0;
 70                 break;
 71             }
 72         }
 73         else if(s[i]==‘?‘ && w[i]==‘?‘)
 74         res = (res*10)%mo;
 75     }
 76     return res%mo;
 77 }
 78
 79 int main()
 80 {
 81      LL i;
 82      LL ans, res1, res2, res3;
 83      while(~scanf("%I64d", &n))
 84      {
 85          scanf("%s%s", s, w);
 86          ans = 1; cnt = 0;
 87          for(i = 0; i < n; i++)
 88          {
 89              if(s[i]==‘?‘) cnt++;
 90              if(w[i]==‘?‘) cnt++;
 91          }
 92          for(i = 0; i < cnt; i++)
 93          ans = (ans*10)%mo;
 94          res1 = cal1();
 95          res2 = cal2();
 96          res3 = cal3();
 97
 98          printf("%I64d\n", (ans-res1-res2+res3+mo+mo)%mo);
 99      }
100      return 0;
101 }
时间: 2024-12-28 04:16:45

Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings (容斥原理)的相关文章

Codeforces Round #129 (Div. 1)E. Little Elephant and Strings

Codeforces Round #129 (Div. 1)E. Little Elephant and Strings 题意:给出n个字符串,问每个字符串有多少个子串(不同位置,相同的子串视作不同)至少出现在这n个字符串中的k个当中. 解法:这题学到了一个SAM的新技能,对于这多个串,建SAM的时候,不是把它们连在一起,建立SAM,而是先给它们建立Trie树,然后广搜这棵Trie树,对于Trie树上的V节点,在建SAM的时候,它应该接在Trie树上他的父亲节点后面,我们用TtoM[U]表示Tr

Codeforces Round #112 (Div. 2) C Another Problem on Strings

题目链接:Codeforces Round #112 (Div. 2) C Another Problem on Strings 题意:给出一个只含0,1的序列,求序列中和为n的子序列有多少个. 思路:预处理出序列的前缀和,然后枚举序列时,记录(vis)该位置之前已有的前缀和,再查询(sum[i]-n)的个数,即以该位置为结束的子序列和为n的个数. 注意:vis数组中0应该始终存在,初始化vis[0]=1(why?,因为sum[i]本身就等于n算一个方法数). 举一反三:类似的就已经可以得到,任

Codeforces Round #179 (Div. 2) B (codeforces 296b) Yaroslav and Two Strings

题目链接:点这里!!!! 题意: 给定两个长度为n(n<=1e5)的串s,w,串中能包含'0'~'9','?','?'可以代替'0'~'9'中任何一个数字. 问你能组成多少对特殊的串('?'代替为数字后的s,w),使得存在(1<=i,j<=n)si<wi,sj>wj. 题解: 1.我们假设'?'的总个数为num,则能够成的总情况为10^num. 2.我们可以通过求相反的情况来求出答案. 3.可以分为4种情况. (1)已知存在(1<=i,j<=n)si<wi,

Codeforces Round #182 (Div. 2)---C. Yaroslav and Sequence(不错的题,分析找规律)

Yaroslav has an array, consisting of (2·n?-?1) integers. In a single operation Yaroslav can change the sign of exactly n elements in the array. In other words, in one operation Yaroslav can select exactly n array elements, and multiply each of them b

Codeforces Round #179 (Div. 2)---C. Greg and Array

Greg has an array a?=?a1,?a2,?-,?an and m operations. Each operation looks as: li, ri, di, (1?≤?li?≤?ri?≤?n). To apply operation i to the array means to increase all array elements with numbers li,?li?+?1,?-,?ri by value di. Greg wrote down k queries

Codeforces Round #179 (Div. 2)---D. Greg and Graph(离线+floyd)

Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game: The game consists of n steps.

Codeforces Round #471 (Div. 2)B. Not simply beatiful strings

Let's call a string adorable if its letters can be realigned in such a way that they form two consequent groups of equal symbols (note that different groups must contain different symbols). For example, ababa is adorable (you can transform it to aaab

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/