Poj 1200 Crazy Search (string hash)

Source: Internet
Author: User

Question:

Analyzes the number of substrings with a length of N.


Train of Thought Analysis:

Assign a number to a character that does not appear.

Then, the substring is treated as the number in the NC hexadecimal format.

Then judge by hash.


#include <cstdio>#include <iostream>#include <algorithm>#include <map>#include <cstring>#include <string>using namespace std;bool vis[26666666];int val[30];char str[1111111];int main(){    int n,nc;    while(scanf("%d%d",&n,&nc)!=EOF)    {        scanf("%s",str);        memset(vis,0,sizeof vis);        memset(val,0,sizeof val);        int len=strlen(str);        int cnt=1;        int ans=0;        for(int i=0;i+n<=len;i++)        {            int sum=0;            for(int j=i;j<i+n;j++)            {                if(!val[str[j]-'a'])val[str[j]-'a']=cnt++;                sum*=nc;                sum+=val[str[j]-'a'];            }            if(!vis[sum])            {                ans++;                vis[sum]=true;            }        }        printf("%d\n",ans);    }    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.