首页 > 其他 > 详细

Codeforces Round #103 (Div. 2)---C. Anagram Search

时间:2015-03-25 12:15:18      阅读:193      评论:0      收藏:0      [点我收藏+]

A string t is called an anagram of the string s, if it is possible to rearrange letters in t so that it is identical to the string s. For example, the string “aab” is an anagram of the string “aba” and the string “aaa” is not.

The string t is called a substring of the string s if it can be read starting from some position in the string s. For example, the string “aba” has six substrings: “a”, “b”, “a”, “ab”, “ba”, “aba”.

You are given a string s, consisting of lowercase Latin letters and characters “?”. You are also given a string p, consisting of lowercase Latin letters only. Let’s assume that a string is good if you can obtain an anagram of the string p from it, replacing the “?” characters by Latin letters. Each “?” can be replaced by exactly one character of the Latin alphabet. For example, if the string p = ?aba?, then the string “a??” is good, and the string ??bc? is not.

Your task is to find the number of good substrings of the string s (identical substrings must be counted in the answer several times).
Input

The first line is non-empty string s, consisting of no more than 105 lowercase Latin letters and characters “?”. The second line is non-empty string p, consisting of no more than 105 lowercase Latin letters. Please note that the length of the string p can exceed the length of the string s.
Output

Print the single number representing the number of good substrings of string s.

Two substrings are considered different in their positions of occurrence are different. Thus, if some string occurs several times, then it should be counted the same number of times.
Sample test(s)
Input

bb??x???
aab

Output

2

Input

ab?c
acb

Output

2

Note

Consider the first sample test. Here the string s has two good substrings: “b??” (after we replace the question marks we get “baa”), “???” (after we replace the question marks we get “baa”).

Let’s consider the second sample test. Here the string s has two good substrings: “ab?” (“?” can be replaced by “c”), “b?c” (“?” can be replaced by “a”).

统计前缀然后枚举区间

/*************************************************************************
    > File Name: CF-103-C.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年03月25日 星期三 10时30分26秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 100110;
char str[N], p[N];
int pre[N];
int sum[N][26];
int numb[26];

int main()
{
    while (~scanf("%s%s", str, p))
    {
        memset(pre, 0, sizeof(pre));
        memset(sum, 0, sizeof(sum));
        memset(numb, 0, sizeof(numb));
        int n = strlen(str);
        int m = strlen(p);
        if (n < m)
        {
            printf("0\n");
            continue;
        }
        for (int i = 1; i <= n; ++i)
        {
            pre[i] = pre[i - 1];
            if (str[i - 1] == ‘?‘)
            {
                ++pre[i];
            }
            for (int j = 0; j < 26; ++j)
            {
                sum[i][j] = sum[i - 1][j];
            }
            if (str[i - 1] >= ‘a‘ && str[i - 1] <= ‘z‘)
            {
                ++sum[i][str[i - 1] - ‘a‘];
            }
        }
        for (int i = 0; i < m; ++i)
        {
            ++numb[p[i] - ‘a‘];
        }
        int ans = 0;
        for (int i = 1; i + m - 1 <= n; ++i)
        {
            bool flag = 1;
            int less = 0;
//          printf("[%d, %d]\n", i, i + m - 1);
            for (int j = 0; j < 26; ++j)
            {
//              printf("p中%c个数%d, str中%c个数%d\n", j + ‘a‘, numb[j], j + ‘a‘, sum[i + m - 1][j] - sum[i - 1][j]);
                if (numb[j] < sum[i + m - 1][j] - sum[i - 1][j])
                {
                    flag = 0;
                    break;
                }
                else if (numb[j] > sum[i + m - 1][j] - sum[i - 1][j])
                {
                    less += numb[j] - (sum[i + m - 1][j] - sum[i - 1][j]);
                }
            }
//          printf("less = %d, flag = %d\n", less, flag);
            if (flag && pre[i + m - 1] - pre[i - 1] == less)
            {
                ++ans;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

Codeforces Round #103 (Div. 2)---C. Anagram Search

原文:http://blog.csdn.net/guard_mine/article/details/44618689

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!