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的位 ...
随机推荐
- Linux查看外网IP
Linux查看外网IP curl cip.cc curl ifconfig.me curl ipinfo.io
- 关于spring事务注解
关于事务的注解常用如下1.如果有事务, 那么加入事务, 没有的话新建一个(默认情况下),也就是当我们要开启事务的时候才用,即有数据库有增删改操作@Transactional(rollbackFor=E ...
- Victor/ArrayList/LinkedList/Stack/CopyOnWriteArrayList 区别
Victor:采用数组的方式存储数据,与ArrayList相同,线程安全.性能比ArrayList差 ArrayList:采用数据的方式存储数据,线程不安全.ArrayList使用数组来存储数据,使用 ...
- Java -- 反射示例
1. 反射: 即加载和解剖, 用于设计框架, 读取配置档 动态创建对象. Person类,用于反射. package com.kevin.class_test; public class Person ...
- 使用POI将doc文件转换为html
需要的jar包有:有一些是依赖包,可以使用maven下载 doc文件转换为html文件 package com.gsww.sxzz.controller.service; import org.apa ...
- Oracle备份和恢复
--什么是脱机备份 脱机备份也叫冷备份,首先管理员使用 shutdown immediate 命令关闭数据库的服务. 之后复制需要的文件,包括数据文件和控制文件等相关内容复制到磁盘的其他位置.当 数据 ...
- Queue 输出数据
坑人的引用类型....输出看看结果是什么 Queue<ParaInfo> strStranList = new Queue<ParaInfo>(); StringBuilder ...
- 分享知识-快乐自己:Shiro 退出登陆清空缓存实现
shiro是一个被广泛使用的安全层框架,通过xml配置方式与spring无缝对接,用户的登陆/退出/权限控制/Cookie等管理系统基础功能交给shiro来管理. 一般,在JavaWEB管理平台系统时 ...
- Unity3D之Mesh(五)绘制圆
前言: Unity3D中Mesh的基本单位是三角形,而圆形就是由许许多多的三角形组成的.那么我们就知道了绘制圆形的Mesh需要两个变量:圆的半径 以及分割数: 一.实现过程 基本过程与之前的类似,最 ...
- dbcc练习1
dbcc tranceon(2588,3604) dbcc ind() dbcc page()