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的位 ...
随机推荐
- EntityFramework 学习 一 Lazy Loading
延迟加载:延迟加载相关的数据 using (var ctx = new SchoolDBEntities()) { //Loading students only IList<Student&g ...
- zookeeper+dubbo问题
1.java.lang.IllegalStateException: Context namespace element 'component-scan' and its parser class [ ...
- 序列化工具类({对实体Bean进行序列化操作.},{将字节数组反序列化为实体Bean.})
package com.dsj.gdbd.utils.serialize; import java.io.ByteArrayInputStream; import java.io.ByteArrayO ...
- Javascript-- jQuery事件篇(1)
jQuery鼠标事件之click与dbclick事件 用交互操作中,最简单直接的操作就是点击操作.jQuery提供了两个方法一个是click方法用于监听用户单击操作,另一个方法是dbclick方法用于 ...
- hdu-1025 Constructing Roads In JGShining's Kingdom(二分查找)
题目链接: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- linux命令学习笔记(59):rcp命令
rcp代表“remote file copy”(远程文件拷贝).该命令用于在计算机之间拷贝文件.rcp命令有两种格式.第一种格式用于文件到文件的拷贝:第二种格式用于把文件或目录拷贝到另一个目录中. . ...
- PS 滤镜— —Twirl Filter
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...
- 【leetcode刷题笔记】Plus One
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- 扩展欧几里得算法(exgcd)
Bezout定理: 对于任意整数a,b,存在一对整数x,y满足:a*x+b*y=gcd(a,b) 证明如下: 在欧几里得算法的最后一步:b=0,即:gcd(a,0)=a 对于b>0,根据欧几里得 ...
- Sublime Text 全程指南(转载)
摘要(Abstract) 本文系统全面的介绍了Sublime Text,旨在成为最优秀的Sublime Text中文教程. 更新记录 2014/09/27:完成初稿 2014/09/28: 更正打开控 ...