11.12 模拟赛T1 加密

【问题描述】

有一种不讲道理的加密方法是: 在字符串的任意位置随机插入字符。 相应的,

不讲道理的解密方法就是从字符串中恰好删去随机插入的那些字符。

给定原文?和加密后的字符串?,求?有多少子串可以通过解密得到原文?。

【输入格式】

输入第一行包含一个字符串?,第二行包含一个字符串?。

【输出格式】

输出一行,包含一个整数,代表可以通过解密得到原文的?的子串的数量。

【样例输入】

abcabcabc
cba

【样例输出】

9

【样例解释】

用[l,r]表示子串开头结尾的下标(从 0 开始编号) ,这 9 种方案是:
[0,6],[0,7],[0,8],[1,6],[1,7],[1,8],[2,6],[2,7],[2,8]

【数据规模和约定】

30%的数据,|t|≤1000。
对于100%的数据,1 ≤ |t| ≤ 300,000,1 ≤|s|≤ 200。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#ifdef unix
#define ll "%lld"
#else
#define ll "%I64d"
#endif
using namespace std;
char s[201],t[300001];
int l1,l2;
long long ans;
int main()
{
    freopen("encrypt.in","r",stdin);
    freopen("encrypt.out","w",stdout);
    int i,j;
    scanf("%s%s",t,s);
    l1=strlen(t);
    l2=strlen(s);
    i=j=0;
    int be=-1,ss=-1;
    while(i<l1)
    {
        if(t[i]==s[0]&&be==-1)
          be=i;
        if(t[i]==s[j])
          j++;
        if(j==l2)
        {
            j=0;
            ans=ans+(long long)(be-ss)*(l1-i);//统计方案数
            i=be;
            ss=be;//防止重复计算
            be=-1;
        }
        i++;
    }
    cout<<ans;
    fclose(stdin);
    fclose(stdout);
    return 0;
}
原文地址:https://www.cnblogs.com/jyhywh/p/6056669.html