Yaroslav and Two Strings
Yaroslav thinks that two strings s and w, consisting of digits and having length n are non-comparable if there are two numbers, i and j (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).
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.
In a single line print the remainder after dividing the answer to the problem by number 1000000007 (109 + 7).
题意: 给定两个字符串 s,w,字符串中“ ? ”表示任意字符,问有多少种方案使 存在 i ,j , si > wi and sj < wj ?
思路: 容斥,先算出总的方案数,即对于每一个‘?' 就乘以10 ,然后再减去不符合情况的,
即当 1. si>wi , sj>=wj 2. sj<wj ,si<= wi
然后加上多减去的,即 si==wj 的情况减了两遍,所以要加上。
#include<bits/stdc++.h>
#define mod 1000000007
using namespace std;
typedef long long ll;
string s1,s2;
int n;
ll work1(ll re1)
{
for(int i=0;i<n;i++)
{
if(s1[i]=='?'&&s2[i]=='?') re1=re1*55%mod;
else if(s1[i]=='?'&&s2[i]!='?') re1=re1*(10-(s2[i]-'0'))%mod;
else if(s1[i]!='?'&&s2[i]=='?') re1=re1*(s1[i]-'0'+1)%mod;
else if(s1[i]-'0'<s2[i]-'0'){ re1=0; break; }
}
return re1%mod;
}
ll work2(ll re2)
{
for(int i=0;i<n;i++)
{
if(s1[i]=='?'&&s2[i]=='?') re2=re2*55%mod;
else if(s1[i]!='?'&&s2[i]=='?') re2=re2*(10-(s1[i]-'0'))%mod;
else if(s1[i]=='?'&&s2[i]!='?') re2=re2*(s2[i]-'0'+1)%mod;
else if(s1[i]-'0'>s2[i]-'0'){ re2=0; break; }
}
return re2%mod;
}
ll work3(ll re3)
{
for(int i=0;i<n;i++)
{
if(s1[i]=='?'&&s2[i]=='?') re3=re3*10%mod;
else if(s1[i]=='?'&&s2[i]!='?') re3=re3%mod;
else if(s1[i]!='?'&&s2[i]=='?') re3=re3%mod;
else if(s1[i]-'0'!=s2[i]-'0'){ re3=0; break; }
}
return re3%mod;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
cin>>s1>>s2;
ll ans=1;
for(int i=0;i<n;i++)
{
if(s1[i]=='?') ans=ans*10%mod;
if(s2[i]=='?') ans=ans*10%mod;
}
ll re1=1,re2=1,re3=1;
ans=(ans-work1(re1)-work2(re2)+mod+work3(re3))%mod;
printf("%lld\n",ans);
}
return 0;
}