字符串子序列问题:

这里,字符串的子序列是字符串中一个或多个字符的串联,而不改变顺序。
一、就单纯的求解字符串有多少子序列,没有别的要求,思路:
  我们可以这么认为,看作字符串的拿取问题,即从母串(长度为n)中拿出字符组成新的子序列,那么每一位的字符都有两种状态,拿与不拿,所以就是2n问题,但是你又不能全都不拿,所以就是2n-1;一般来说,就提前打表,该取余就取余;
二、不单纯的求解,即存在重复字符的处理问题:
  1、给你一个长度为n的字符串。统计子序列个数,模109+7,子序列要求所有的字符都是不同的。如果两个子序列的字符来自字符串中的不同位置,则认为它们是不同的,即使它们与字符串相同。
  这种问题,可以看作每种字符的拿取问题,因为它的子序列只能包含不同种类的字符,假设字符’a’在母串出现的次数是m,那么对于’a’的拿取就有m+1种情况,为什么是m+1呢?首先题目要求:如果两个子序列的字符来自字符串中的不同位置,则认为它们是不同的,即使它们与字符串相同再加上可以不拿’a’刚好m+1种情况;
  然后依次找出每种字符出现的次数m1,m2,m3……me,那么出现次数就应该是ans = m1 * m2 * m3 * …… * me - 1;-1还是去除空串的情况
例题:
A - Colorful Subsequence
Time Limit: 2 sec / Memory Limit: 1024 MB
Problem Statement
You are given a string S of length N. Among its subsequences, count the ones such that allcharacters are different, modulo 109+7. Two subsequences are considered different if their characters come from different positions in the string, even if they are the same as strings.
Here, a subsequence of a string is a concatenation of one or more characters from the string without changing the order.
Constraints
1≤N≤100000
S consists of lowercase English letters.
|s| = N
Input
Input is given from Standard Input in the following format:
N
S
Output
Print the number of the subsequences such that all characters are different, modulo 109+7.
Sample Input 1
4
abcd
Sample Output 1
15
Since all characters in S itself are different, all its subsequences satisfy the condition.
Sample Input 2
3
baa
Sample Output 2
5
The answer is five: b, two occurrences of a, two occurrences of ba. Note that we do not count baa, since it contains two as.
Sample Input 3
5
abcab
Sample Output 3
17

#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<string>
#define ll long long
#define mod 1000000007
using namespace std;
ll a[27];
string s;
int main(void)
{
    int n;
    ll ans;
    scanf("%d",&n);
    cin>>s;
    for(int i=0;i<27;i++)
        a[i] = 1;
    for(int i=0;i<n;i++)
        a[s[i]-'a']++;
    ans = 1;
    for(int i=0;i<27;i++)
        ans = (ans*a[i]) % mod;

    printf("%lld\n",ans-1);
    return 0;
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逃夭丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值