[Hash] POJ 1200 Crazy Search

 POJ 1200 Crazy Search

  • The meaning of problems: there is a string, the number of different characters in the string may occur is nc, let us ask how many different sub-length n string string there (believe big Karma should know substring and subsequence the difference [every time I write a blog not have to go to Baidu www, afraid pan qaq]).
  •  You may assume that the maximum number of substrings formed by the possible set of characters does not exceed 16 Millions. [Character may be formed of different sub-strings ( simply do not define the input string ) is the maximum number of not more than 16 Million. ] this last condition is simply a question of a number of important eye ah www. Means that we will be the string as "large numbers", then the scope of large numbers is 16e6. If the topic given is 211, so that is the number of different sub-strings of no more than 211 ah, 1,2,3,4,5, ..., 10, 11, these are the different sub-strings. QAQ. The beginning has been read wrong, I do not know what the hell is this range in the end.
  • Thinking: we will all the different characters from the 1-nc number, so we are nc + 1 decimal (nc band can be, but after all, is 1-nc, 0 although there is no use, but still nc + 1 decimal thing) , open a vis [] array of a hash value recorded there occurred. Then we again sweep substring of length n, and calculates a hash value, and then to record ans.
  • I do not know if I did not like nc with this condition, directly on the string matching algorithm karp-rabin template. Too can not. I MLE had also been WA. 16M is probably because we have to limit the use nc hex. I direct a 27-ary or wrong, may be 27 hex card.
  • There is the set will be T, set will be T, set will T !!!!!!! qaq

FEELING: at first glance is really hash Well, again swept past, the hash value is the number of how many different sub-strings slightly. The first is karp-rabin string matching algorithm directly with (their own definition of the base, so there is no use nc). emmmMLE up, www. After reading this question of discuss, he says a good look at the title, is to use the nc slightly, so he changed nc hex. A right or wrong ah WA. Later solution to a problem is found, all is not seeking a hash value range, that is, no pretreatment. It is considered when traversing the hash value. Then opened a vis array. This time the problem came. This array open vis how much? Why you can open 16e6? Later, people asked it, his word awoke me, because we are seen as a string of hexadecimal nc "large numbers", so we substring is the number of large numbers is the extent. For example, 211, 211 of the substring then there are 211. Anyway, I still think this question is very strange. I do not understand, do not understand. www

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) x & (-x)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 16000000 + 7;
const int base = 27;
int n, nc;
char s[maxN];
int disc[26];
bool vis[maxN];
void init()
{
    memset(disc, 0, sizeof(disc));
    memset(vis, false, sizeof(vis));
}
int main()
{
    while(~scanf("%d%d", &n, &nc))
    {
        init();
        getchar();
        scanf("%s", s);
        int len = strlen(s);
        int tot = 1;
        for(int i = 0; i < len; i ++ )
        {
            if(!disc[s[i] - 'a'])
                disc[s[i] - 'a'] = tot ++;
            if(tot > nc) break;
        }
        int ans = 0;
        for(int i = 0; i <= len - n; i ++ )//枚举长度为n的字符串的开头
        {
            int Hash = 0;
            for(int j = i; j < i + n; j ++ )
                Hash = Hash * tot + disc[s[j] - 'a'];
            if(!vis[Hash])
            {
                vis[Hash] = true;
                ans ++;
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

He published 191 original articles · won praise 57 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44049850/article/details/104079945