2013
12-09

# Crazy Search

Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could be finding a hidden prime number in a given text. Such number could be the number of different substrings of a given size that exist in the text. As you soon will discover, you really need the help of a computer and a good algorithm to solve such a puzzle.

Your task is to write a program that given the size, N, of the substring, the number of different characters that may occur in the text, NC, and the text itself, determines the number of different substrings of size N that appear in the text.

As an example, consider N=3, NC=4 and the text "daababac". The different substrings of size 3 that can be found in this text are: "daa", "aab", "aba", "bab", "bac". Therefore, the answer should be 5.

The first line of input consists of two numbers, N and NC, separated by exactly one space. This is followed by the text where the search takes place. You may assume that the maximum number of substrings formed by the possible set of characters does not exceed 16 Millions.

The program should output just an integer corresponding to the number of different substrings of size N found in the given text.
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

1

3 4
daababac

5

#include <stdio.h>
#include <string.h>
#define N 16000007
int hash[8000000],assi[27];
char str[N];
int a[N];
int main()
{
#ifndef ONLINE_JUDGE
freopen("acm.txt","r",stdin);
#endif
int T,n,nc,len,k;
int i,j,sum;
scanf("%d",&T);
while(T--)
{
memset(hash,0,sizeof(hash));
scanf("%d %d",&n,&nc);
getchar();
scanf("%s",str);
len=strlen(str);
i=0;
k=0;
for(i = 0; i < len; i++)
{
a[i] = str[i] - 'a';
}
i = 0;
while(str[i]!='\0')
{
if(assi[a[i]]==0)
assi[a[i]]=k++;
if(k==nc)
break;
i++;
}
int cnt=len-n+1;
for(i=0;i<len-n+1;i++)
{
sum=0;
for(j=i;j<i+n;j++)
{
sum=sum*nc+assi[a[j]];
}
sum%=N;
if(hash[sum]==0)
hash[sum]=1;
else
cnt--;
}
printf("%d\n",cnt);
if(T>0)
printf("\n");
}
return 0;
}

1. /*
* =====================================================================================
*
* Filename: 1366.cc
*
* Description:
*
* Version: 1.0
* Created: 2014年01月06日 14时52分14秒
* Revision: none
* Compiler: gcc
*
* Author: Wenxian Ni (Hello World~), [email protected]
* Organization: AMS/ICT
*
* =====================================================================================
*/

#include
#include

using namespace std;

int main()
{
stack st;
int n,i,j;
int test;
int a[100001];
int b[100001];
while(cin>>n)
{
for(i=1;i>a[i];
for(i=1;i>b[i];
//st.clear();
while(!st.empty())
st.pop();
i = 1;
j = 1;

while(in)
break;
}
while(!st.empty()&&st.top()==b[j])
{
st.pop();
j++;
}
}
if(st.empty())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}