codeforces 895d string mark

D. String Mark
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

At the Byteland State University marks are strings of the same length. Mark x is considered better than y if string y is lexicographically smaller than x.

Recently at the BSU was an important test work on which Vasya recived the mark a. It is very hard for the teacher to remember the exact mark of every student, but he knows the mark b, such that every student recieved mark strictly smaller than b.

Vasya isn't satisfied with his mark so he decided to improve it. He can swap characters in the string corresponding to his mark as many times as he like. Now he want to know only the number of different ways to improve his mark so that his teacher didn't notice something suspicious.

More formally: you are given two strings ab of the same length and you need to figure out the number of different strings c such that:

1) c can be obtained from a by swapping some characters, in other words c is a permutation of a.

2) String a is lexicographically smaller than c.

3) String c is lexicographically smaller than b.

For two strings x and y of the same length it is true that x is lexicographically smaller than y if there exists such i, that x1 = y1, x2 = y2, ..., xi - 1 = yi - 1, xi < yi.

Since the answer can be very large, you need to find answer modulo 109 + 7.

Input

First line contains string a, second line contains string b. Strings a, b consist of lowercase English letters. Their lengths are equal and don't exceed 106.

It is guaranteed that a is lexicographically smaller than b.

Output

Print one integer  — the number of different strings satisfying the condition of the problem modulo 109 + 7.

Examples
input
Copy
abc
ddd
output
5
input
Copy
abcdef
abcdeg
output
0
input
Copy
abacaba
ubuduba
output
64
Note

In first sample from string abc can be obtained strings acb, bac, bca, cab, cba, all of them are larger than abc, but smaller than ddd. So the answer is 5.

In second sample any string obtained from abcdef is larger than abcdeg. So the answer is 0.

题意:给一个字符串s,一个字符串t,求s全部字符都选的不同组合中字典序大于s小于t的字符串有多少种。

首先我们转换下题意,可以转换成求小于t的-小于s的,那么就比较好求了。

我们用dp[i]表示到第i位的时候的方案数,那么直接枚举这一位的字母,然后用乘法原理和阶乘转移即可,这里我写的是迭代的方式,其实递归的更好理解。

记得答案-1,就是减去了s,因为算小于t的时候我们把s算上了。

代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
char aa[1000005],bb[1000005];
int fac[1000005],inv[1000005],f[505];
int pow(int a,int b)
{
	ll di=a,mi=b,res=1;
	for(;mi;mi>>=1)
	{
		if(mi&1) res=res*di%1000000007;
		di=di*di%1000000007;
	}
	return res;
}
int cnt(char arr[],int n)
{
    int res=0;
    for(int i=0;i<26;i++) f[i]=0;
    for(int i=1;i<=n;i++) f[aa[i]-'a']++;
    for(int i=1;i<=n;i++)
    {
        int val=arr[i]-'a',tmp=fac[n-i];
        for(int j=0;j<26;j++)tmp=(ll)tmp*inv[f[j]]%1000000007;
        for(int j=0;j<val;j++)
            if(f[j])
            {
                ll tmp2=(ll)tmp*fac[f[j]]%1000000007*inv[f[j]-1]%1000000007;
                res=(res+tmp2)%1000000007;
            }
        if(f[val]==0)break;
        f[val]--;
    }
    return res;
}
int main()
{
    cin>>(aa+1)>>(bb+1);
    int n=strlen(aa+1);
    fac[0]=1;
    for(int i=1;i<=n;i++) fac[i]=(ll)i*fac[i-1]%1000000007;
    for(int i=0;i<=n;i++) inv[i]=pow(fac[i],1000000007-2);
    cout<<((ll)cnt(bb,n)-cnt(aa,n)-1+1000000007)%1000000007<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值