hdu 4821

String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1410    Accepted Submission(s): 414


Problem Description
Given a string S and two integers L and M, we consider a substring of S as “recoverable” if and only if
  (i) It is of length M*L;
  (ii) It can be constructed by concatenating M “diversified” substrings of S, where each of these substrings has length L; two strings are considered as “diversified” if they don’t have the same character for every position.

Two substrings of S are considered as “different” if they are cut from different part of S. For example, string "aa" has 3 different substrings "aa", "a" and "a".

Your task is to calculate the number of different “recoverable” substrings of S.
 
Input
The input contains multiple test cases, proceeding to the End of File.

The first line of each test case has two space-separated integers M and L.

The second ine of each test case has a string S, which consists of only lowercase letters.

The length of S is not larger than 10^5, and 1 ≤ M * L ≤ the length of S.
 
Output
For each test case, output the answer in a single line.
 
Sample Input
3 3
abcabcbcaabc
 
Sample Output
2
题目意思:问存在多少个子串,长度为n*l,而且这个子串由n个不同的 且长度都为l的串构成
直接hash长度为l的串就可以了
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string.h>
 4 #include<vector>
 5 #include<algorithm>
 6 #include<map>
 7 using namespace std;
 8 typedef unsigned long long ull;
 9 #define mmax 100000+10
10 ull base[mmax],seed=131,ansx[mmax],ansy[mmax],aans[mmax];
11 bool vit[mmax],vit2[mmax];
12 map<ull,int>to;
13 int n,m;
14 char p[mmax];
15 int main(){
16     base[0]=1;
17     for(int i=1;i<=100000;i++) base[i]=base[i-1]*seed;
18     while(cin>>n>>m){
19         scanf("%s",p);
20         int len=strlen(p),tmp=n*m,id=0;
21         memset(ansx,0,sizeof(ansx));
22         for(int i=0;i<m;i++)
23         ansx[0]=ansx[0]*seed+p[i];
24         for(int i=m;i<len;i++) ansx[i-m+1]=ansx[i-m]*seed-p[i-m]*base[m]+p[i];
25         memset(ansy,0,sizeof(ansy));
26         for(int i=0;i<tmp;i++)
27         ansy[0]=ansy[0]*seed+p[i];
28         for(int i=tmp;i<len;i++) ansy[i-tmp+1]=ansy[i-tmp]*seed-p[i-tmp]*base[tmp]+p[i];
29         memset(aans,0,sizeof(aans));
30         for(int i=0;i<m;i++){
31             to.clear();
32             int first=i;
33             for(int j=i,num=0;j<len+m;j+=m,num++){
34                 if(num>=n){
35                     if(to.size()==n) {
36                         aans[id++]=ansy[first];
37                     }
38                     to[ansx[first]]--;
39                     if(!to[ansx[first]]) to.erase(ansx[first]);
40                     first+=m;
41                 }
42                 to[ansx[j]]++;
43             }
44         }
45         sort(aans,aans+id);
46 //        for(int i=0;i<id;i++) cout<<aans[i]<<" ";
47 //        cout<<endl;
48         if(id==0){
49             cout<<0<<endl;
50             continue;
51         }
52         int sum=0;
53         for(int i=0;i<id;i++){
54             if(aans[i]) {sum++;continue;}
55 
56         }
57         cout<<sum<<endl;
58     }
59 }
 
原文地址:https://www.cnblogs.com/ainixu1314/p/4227125.html