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
2
90
09
1
2
11
55
0
5
?????
?????
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 (容斥原理)的更多相关文章
- Codeforces Round #179 (Div. 1 + Div. 2)
A. Yaroslav and Permutations 值相同的个数不能超过\(\lfloor \frac{n + 1}{2} \rfloor\). B. Yaroslav and Two Stri ...
- 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 ...
- 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 ...
- Codeforces Round #179 (Div. 1)
A 直接线段树过的 两遍 貌似大多是标记过的..注意long long #include <iostream> #include <cstdio> #include <c ...
- 字符串(后缀自动机):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 ...
- 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 ...
- Codeforces Round #129 (Div. 1)E. Little Elephant and Strings
题意:有n个串,询问每个串有多少子串在n个串中出现了至少k次. 题解:sam,每个节点开一个set维护该节点的字符串有哪几个串,启发式合并set,然后在sam上走一遍该串,对于每个可行的串,所有的fa ...
- 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 ...
- Codeforces Round #112 (Div. 2)
Codeforces Round #112 (Div. 2) C. Another Problem on Strings 题意 给一个01字符串,求包含\(k\)个1的子串个数. 思路 统计字符1的位 ...
随机推荐
- Vim 的命令模式转插入模式
一.在命令模式输入下面的快捷方式: i 在当前光标前插入字符: I 在当前行行首插入字符: a 在当前光标后插入字符: A 在当前行行尾插入字符: o 在当前行下面另起一新行: O 在当前行上面另起一 ...
- Oracle数据库设计规范建议
Oracle数据库设计规范建议 1 目的 本规范的主要目的是希望规范数据库设计,尽量提前避免由于数据库设计不当而产生的麻烦:同时好的规范,在执行的时候可以培养出好的习惯,好的习惯是软件质量的很好的保证 ...
- 在Treeview中节点的data属性中保存记录类型及其消除的办法
一.保存记录类型在data指针中: procedure TForm1.getheaditems(pp:TfrxBand;hnode:THeadTreeNode;var i:Integer;var j: ...
- RSA加密方法java工具类
package com.qianmi.weidian.common.util; import java.io.ByteArrayOutputStream; import java.security.K ...
- C++拷贝构造函数(深拷贝,浅拷贝)
http://www.cnblogs.com/BlueTzar/articles/1223313.html C++拷贝构造函数(深拷贝,浅拷贝) 对于普通类型的对象来说,它们之间的复制是很简单的,例如 ...
- php设计模式课程---4、观察者模式的好处是什么
php设计模式课程---4.观察者模式的好处是什么 一.总结 一句话总结: 方便选择之后去控制监听的板块数:比如选择男士之后,我可以决定监听广告里面的第二和第三板块. 1.为什么有观察者模式? 错误理 ...
- orcal操作锦集
更新时间:update qs_settle_dt_cfg set end_date=to_date('9999-12-31','yyyy-MM-dd');查询时间:select to_char( e ...
- 分享知识-快乐自己:快速理解(Java内部类)
1):成员内部类 什么是内部类?: 内部类就是在一个类中定义另一个类. 定义语法: 使用命令行查看编译 产生的文件: 如何生成内部类对象? 创建规则:内部类对象 需要先声明外部类对象. 内部类以及外部 ...
- 关于ios::sync_with_stdio(false);和 cin.tie(0)加速c++输入输出流
原文地址:http://www.hankcs.com/program/cpp/cin-tie-with-sync_with_stdio-acceleration-input-and-output.ht ...
- Git_错误_02_error: src refspec master does not match any
现象:在一个目录下初始化仓库之后,就开始push到github,结果出现了这个错误. 错因:初始化仓库之后,并没有使用git add,git commit 命令将文件添加到git仓库中,所以仓库为空, ...