题目链接

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
2
90
09
Output
1
Input
2
11
55
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的情况。

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

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define LL __int64
const int maxn = 1e5 + ;
const LL mo = 1e9 + ;
using namespace std;
char s[maxn], w[maxn];
LL n, cnt; LL cal1()
{
LL i, res = ;
for(i = ; i < n; i++)
{
if(s[i]!='?' && w[i]!='?')
{
if(s[i]<w[i])
{
res = ;
break;
}
}
else if(s[i]=='?' && w[i]=='?')
res = (res*)%mo;
else if(s[i]=='?')
res = (res*(-w[i]+))%mo;
else
res = (res*(s[i]-+))%mo;
}
return res%mo;
} LL cal2()
{
LL i, res = ;
for(i = ; i < n; i++)
{
if(s[i]!='?' && w[i]!='?')
{
if(s[i]>w[i])
{
res = ;
break;
}
}
else if(s[i]=='?' && w[i]=='?')
res = (res*)%mo;
else if(s[i]=='?')
res = (res*(w[i]-+))%mo;
else
res = (res*(-s[i]+))%mo;
}
return res%mo;
} LL cal3()
{
LL i, res = ;
for(i = ; i < n; i++)
{
if(s[i]!='?' && w[i]!='?')
{
if(s[i]!=w[i])
{
res = ;
break;
}
}
else if(s[i]=='?' && w[i]=='?')
res = (res*)%mo;
}
return res%mo;
} int main()
{
LL i;
LL ans, res1, res2, res3;
while(~scanf("%I64d", &n))
{
scanf("%s%s", s, w);
ans = ; cnt = ;
for(i = ; i < n; i++)
{
if(s[i]=='?') cnt++;
if(w[i]=='?') cnt++;
}
for(i = ; i < cnt; i++)
ans = (ans*)%mo;
res1 = cal1();
res2 = cal2();
res3 = cal3(); printf("%I64d\n", (ans-res1-res2+res3+mo+mo)%mo);
}
return ;
}

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

  1. Codeforces Round #179 (Div. 1 + Div. 2)

    A. Yaroslav and Permutations 值相同的个数不能超过\(\lfloor \frac{n + 1}{2} \rfloor\). B. Yaroslav and Two Stri ...

  2. Codeforces Round #182 (Div. 1) B. Yaroslav and Time 最短路

    题目链接: http://codeforces.com/problemset/problem/301/B B. Yaroslav and Time time limit per test2 secon ...

  3. Codeforces Round #179 (Div. 1) A. Greg and Array 离线区间修改

    A. Greg and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/295/pro ...

  4. Codeforces Round #179 (Div. 1)

    A 直接线段树过的 两遍 貌似大多是标记过的..注意long long #include <iostream> #include <cstdio> #include <c ...

  5. 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings

    E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ...

  6. Codeforces Round #272 (Div. 1) Problem C. Dreamoon and Strings

    C. Dreamoon and Strings time limit per test 1 second memory limit per test 256 megabytes input stand ...

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

    题意:有n个串,询问每个串有多少子串在n个串中出现了至少k次. 题解:sam,每个节点开一个set维护该节点的字符串有哪几个串,启发式合并set,然后在sam上走一遍该串,对于每个可行的串,所有的fa ...

  8. 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 conseq ...

  9. Codeforces Round #112 (Div. 2)

    Codeforces Round #112 (Div. 2) C. Another Problem on Strings 题意 给一个01字符串,求包含\(k\)个1的子串个数. 思路 统计字符1的位 ...

随机推荐

  1. Struts2 内核之我见

    Struts2 内核之我见 完整分析 Struts2 内核中文文档 本文首先探讨了 Struts2 核心控制器的源码,以帮助解读 Struts2 的工作流程.接着讲解相关外围类.最后对 Struts ...

  2. org.eclipse.core.resources.bak文件导致MyEclipse每次关闭时无法保存文件

    MyEclipse关闭时提示如下信息 Problems occurred while trying to save the state of the workbench. Internal Error ...

  3. Mysql的链接超时异常CommunicationsException

    原文是在博客上的:小重合之旅 链接如下:未经过作者同意,这里注明下. http://blog.csdn.net/bluesnail216/article/details/15810119 1,问题现象 ...

  4. 本地文件读取(csv,txt)时字符编码问题解决

    今天进行csv文件读取时,老是入库为空,因为其中有中文字符,我要通过中文字符映射成相应的编号(上升:1011,下降:1012),于是怎么也取不到编号.刚开始以为程序映射出了问题,最后日志打出来后,发现 ...

  5. Codeforces 455B A Lot of Games:博弈dp【多局游戏】

    题目链接:http://codeforces.com/problemset/problem/455/B 题意: 给你n个字符串,然后进行k局游戏. 每局游戏开始有一个空串,然后双方轮流给这个串的末尾添 ...

  6. Codeforces 351B Jeff and Furik:概率 + 逆序对【结论题 or dp】

    题目链接:http://codeforces.com/problemset/problem/351/B 题意: 给你一个1到n的排列a[i]. Jeff和Furik轮流操作,Jeff先手. Jeff每 ...

  7. 十四 Django框架,中间件

    django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项目的se ...

  8. 2013面试C++小结

    2013年我在厦门c++求职小结 1.一般公司出的面试题目中的找错误,都是出自平常公司内部使用过程中出现的真实错误. 比如stl 中erase的使用:详细请见 :http://blog.csdn.ne ...

  9. 使用kill命令终止进程shell脚本

    因有的程序使用kill才能结束掉进程,没有关闭脚本,以我司的服务为例,服务名叫asset-server服务,只有启动脚本,自编写关闭脚本,及重启动脚本. 关闭服务脚本. vim asset-shutd ...

  10. MySQL--开发技巧(一)

    Inner Join: Left Outer Join: Right Outer Join: Full Join: Cross Join: SELECT t1.attrs ,t2.attrs FROM ...