zoukankan      html  css  js  c++  java
  • String(hdu 4821 字符串Hash+Map)

    String

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


    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
     
    Source
     
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <map>
     6 #define ull unsigned long long   //溢出部分自动取余
     7 using namespace std;
     8 
     9 int m,l;
    10 int base=133;
    11 const int maxn=1e5+5;
    12 ull p[maxn],Hash[maxn];    //用ull类型
    13 map<ull,int> ma;
    14 string name;
    15 
    16 void init(){
    17     p[0]=1;
    18     for(int i=1;i<=maxn-1;i++) p[i]=p[i-1]*base;
    19 }    //初始化
    20 
    21 ull get(int left,int right,ull g[]){
    22     return g[right]-g[left-1]*p[right-left+1];
    23 }
    24 
    25 int main(){
    26     init();
    27     while(cin>>m>>l){
    28         cin>>name;
    29         int len=name.size();
    30         name=" "+name;
    31         Hash[0]=0;
    32         for(int i=1;i<=len;i++){
    33             Hash[i]=Hash[i-1]*base+name[i]-'a';   //对字符串进行Hash
    34         }
    35         int res=0;
    36         for(int i=1;i<=l&&i+m*l<=len;i++){
    37             ma.clear();
    38             for(int j=i;j<i+m*l;j+=l){
    39                 ull x=get(j,j+l-1,Hash);
    40                 ma[x]++;
    41             }
    42             if(ma.size()==m) res++;
    43             for(int j=i+m*l;j+l-1<=len;j+=l){   //头部删除,尾部加入
    44                 ull x=get(j,j+l-1,Hash);
    45                 ma[x]++;
    46                 ull y=get(j-m*l,j-m*l+l-1,Hash);
    47                 ma[y]--;
    48                 if(ma[y]==0) ma.erase(y);   //不要忘记删除
    49                 if(ma.size()==m) res++;
    50             }
    51         }
    52         cout << res << endl;
    53     }
    54     return 0;
    55 }
    View Code
  • 相关阅读:
    数据访问技术系列课程 笔记(2) ADO.NET 连接方式进行数据访问
    Modern C# 系列课程笔记 第11节 深入委托和事件
    idea 将项目托管到 Git 报错:Can't finish Gitee sharing process
    ADO.Net
    WebService
    2013年了
    201301杂谈
    流程图
    出错列表
    杂谈4 2012年8月15日开
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11698775.html
Copyright © 2011-2022 走看看