zoukankan      html  css  js  c++  java
  • CF1272C

    Recently, Norge found a string s=s1s2sns=s1s2…sn consisting of nn lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string ss. Yes, all n(n+1)2n(n+1)2 of them!

    A substring of ss is a non-empty string x=s[ab]=sasa+1sbx=s[a…b]=sasa+1…sb (1abn1≤a≤b≤n). For example, "auto" and "ton" are substrings of "automaton".

    Shortly after the start of the exercise, Norge realized that his keyboard was broken, namely, he could use only kk Latin letters c1,c2,,ckc1,c2,…,ck out of 2626.

    After that, Norge became interested in how many substrings of the string ss he could still type using his broken keyboard. Help him to find this number.

    Input

    The first line contains two space-separated integers nn and kk (1n21051≤n≤2⋅105, 1k261≤k≤26) — the length of the string ss and the number of Latin letters still available on the keyboard.

    The second line contains the string ss consisting of exactly nn lowercase Latin letters.

    The third line contains kk space-separated distinct lowercase Latin letters c1,c2,,ckc1,c2,…,ck — the letters still available on the keyboard.

    Output

    Print a single number — the number of substrings of ss that can be typed using only available letters c1,c2,,ckc1,c2,…,ck.

    Examples
    input
    7 2
    abacaba
    a b
    
    output
    12
    
    input
    10 3
    sadfaasdda
    f a d
    
    output
    21
    
    input
    7 1
    aaaaaaa
    b
    
    output
    0
    
    Note

    In the first example Norge can print substrings s[12]s[1…2], s[23]s[2…3], s[13]s[1…3], s[11]s[1…1], s[22]s[2…2], s[33]s[3…3], s[56]s[5…6], s[67]s[6…7], s[57]s[5…7], s[55]s[5…5], s[66]s[6…6], s[77]s[7…7].


    题倒是简单但是s=200000这样的数据用int就中招了

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #define LL long long
    using namespace std;
    
    
    int main()
    {
        int n, k;
        cin >> n >> k;
        string s;
        cin >> s;
        set<char> all;
        for (int i = 0; i < k; i++)
        {
            char t;
            cin >> t;
            all.insert(t);
        }
        LL cnt=0,sum=0;
        for (int i = 0; i < s.size(); i++)
        {
            if (all.find(s[i]) != all.end())
            {
                cnt++;
            }
            else
            {
                sum += cnt*(cnt + 1) / 2;
                cnt = 0;
            }
            if(i==s.size()-1 && all.find(s[i]) != all.end())
                sum += cnt*(cnt + 1) / 2;
        }
        cout << sum;
        //system("pause");
        return 0;
    }
  • 相关阅读:
    ASCII码对照表
    createPopup 超链接
    说说回车键触发表单提交的问题
    linux下配java环境的小结
    spring bind checkbox 传递值问题
    用Common validator为springMVC做验证时遇到的一个问题小记
    [转载]对android LinearLayout中layout_weight属性使用初探
    linux下tomcat启动正常,但用http://22.22.33.33:8080却访问不了,防火墙的设置问题
    Java 遍历Map时 删除元素
    ftp用户登录时不能进自己的目录,被拒绝登录的解决方法
  • 原文地址:https://www.cnblogs.com/dealer/p/12269004.html
Copyright © 2011-2022 走看看