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的位 ...
 
随机推荐
- FOJ 2213 简单几何
			
题意:给你两个圆的圆心坐标和半径,判断两个圆公切线数目. 思路:考虑两个圆间公切线的情况,两个圆的位置关系分为相离,相交,外切,内切,内含,重合,公切线数分别为4,2,3,1,0,-1. #inclu ...
 - Nginx Rewrite语法详解
			
重写中用到的指令 if (条件) {} 设定条件,再进行重写 set #设置变量 return #返回状态码 return 403; break #跳出rewrite rewrite #重写 I ...
 - Struts2的工作流程分析
			
Struts2的工作流程分析 Posted on 2011-02-22 09:32 概述 本章讲述Struts2的工作原理. 读者如果曾经学习过Struts1.x或者有过Struts1.x的开发经验, ...
 - 每个程序员都应该了解的 CPU 高速缓存
			
每个程序员都应该了解的 CPU 高速缓存 英文原文:Memory part 2: CPU caches 来源:oschina [编者按:这是Ulrich Drepper写“程序员都该知道存储器”的第二 ...
 - JQUERY获取html标签自定义属性值或data值
			
//获取属性值 1 <div id="text" value="黑哒哒的盟友"><div> jQuery取值: $("#tex ...
 - vim乱码的解决
			
解决vim文件乱码,打开文件乱码,菜单,提示信息乱码: 有四个跟字符编码方式有关的选项,encoding.fileencoding.fileencodings.termencoding 在linux中 ...
 - Java_基础_01_static和final
			
二.参考资料 1.java入门之关键字static和final 2.static和final的区别
 - Git_学习_05_ 解决冲突
			
二.参考资料 1.使用git pull文件时和本地文件冲突怎么办? 2.git 冲突解决
 - 绘图工具--turtle模块
			
turtle模块主要使用两个类,一个是TurtleScreen类,表示画布(窗口),用来展示画的位置:一个是Turtle类,用来充当画笔,用来画. 两个类的方法也以同名的函数的形式存在,所以可以以面向 ...
 - Java中的泛型、枚举和注解
			
1.泛型: 一.为什么要有泛型(Generic)?1.解决元素存储的安全性问题任何类型都可以添加到集合中:类型不安全2.解决获取数据元素时,需要类型强转的问题读取出来的对象需要强转:繁琐可能有Clas ...