Spoj_dsubseq distinct subsequences

Source: Internet
Author: User

Count the number of substrings of a string consisting of only uppercase letters. This indicates that substrings are not subsequences and can be discontinuous. Note that

Then, according to the counting DP idea, DP [I] [J] indicates a substring with the length of I, and the last letter is J.

Then, in order to deduplicate each DP, record the number of digits of the last digit at this time, and use a suffix to record whether or not the letter exists, from the last J location next look at the suffix has this letter, if valid DP [I] [J] + = DP [I-1] [K].

However, this method has a very large vulnerability, that is, de-duplication. To prevent duplication, although I use a suffix to record whether there is a letter behind it, when I add it, I am unified, that is, no matter whether there is a letter behind, I directly unified + dp [I-1] [J], resulting in the results to the end of the failure, and this method will time out, although I was expecting him to time out, I did not expect to return a wa.

 

In fact, it may be because of the results of the recent increase in the count of DP. This is actually done in one dimension. Scan the current letter from the past to the next, my value is the new substring generated after DP [I-1] + adding the current letter

The number of new substrings is divided into two situations,

1. The letter has not appeared before, then the new number = DP [I-1] + 1, indicating that the current letter plus the previous DP [I-1] strings can produce new substrings, + 1 refers to a single letter.

2. the letter appeared before, it is to judge the repetition, in fact, is DP [I-1]-DP [lastoccur-1]; that is, add DP [I-1] But there must be a repetition, to remove duplicates, find the place where the letter appeared last time, and remove the place to remove duplicates.

It should be noted that there is a subtraction in this section, and the answer is to take the modulo, so this subtraction may be a negative number (this place was not expected before, did not pay attention, I am still wondering why other people have an operation to determine negative values.) This is because there is a subtraction in the modulo, so negative numbers may occur. Pay attention to this situation.

#include <cstdio>#include <iostream>#include <cstring>#define LL long longusing namespace std;const int N = 100010;const LL M = 1000000007;char str[N];LL dp[N];int lasts[30];int main(){    int t;    scanf("%d",&t);    while (t--)    {        scanf("%s",str+1);        int len=strlen(str+1);        for (int i=0;i<=len;i++){            dp[i]=0;        }        for (int i=0;i<30;i++) lasts[i]=0;        for (int i=1;i<=len;i++){            dp[i]=dp[i-1];            if (lasts[str[i]-‘A‘]>0){                dp[i]+=dp[i-1]-dp[lasts[str[i]-‘A‘]-1];                while (dp[i]<0) dp[i]+=M;            }            else{                dp[i]+=dp[i-1]+1;            }            lasts[str[i]-‘A‘]=i;            if (dp[i]>=M) dp[i]%=M;        }        dp[len]++;        dp[len]%=M;        printf("%lld\n",dp[len]);    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.