PAT TOP 1005 Programming Pattern (35 分)哈希做法

3 篇文章 0 订阅
1 篇文章 0 订阅

 

1005 Programming Pattern (35 分)

Programmers often have a preference among program constructs. For example, some may prefer if(0==a), while others may prefer if(!a). Analyzing such patterns can help to narrow down a programmer's identity, which is useful for detecting plagiarism.

Now given some text sampled from someone's program, can you find the person's most commonly used pattern of a specific length?

Input Specification:

Each input file contains one test case. For each case, there is one line consisting of the pattern length N (1≤N≤1048576), followed by one line no less than N and no more than 1048576 characters in length, terminated by a carriage return \n. The entire input is case sensitive.

Output Specification:

For each test case, print in one line the length-N substring that occurs most frequently in the input, followed by a space and the number of times it has occurred in the input. If there are multiple such substrings, print the lexicographically smallest one.

Whitespace characters in the input should be printed as they are. Also note that there may be multiple occurrences of the same substring overlapping each other.

Sample Input 1:

4
//A can can can a can.

Sample Output 1:

 can 4

Sample Input 2:

3
int a=~~~~~~~~~~~~~~~~~~~~~0;

Sample Output 2:

~~~ 19

code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ull B=100000007;
int N;
string str;
int mp[10000100];
bool lower(int i,int j)
{
    for(int t=0;t<N;t++)
        if(str[i+t]!=str[j+t])return str[i+t]<str[j+t];
    return true;
}
int main()
{
    cin>>N;cin.ignore();
    getline(cin,str);
    ull hash=0;
    for(int i=0;i<N;i++)
    {
        hash=hash*B+str[i];
    }
    int len=str.length();
    ull t=1;
    for(int i=0;i<N;i++)t*=B;
    int ans=0;vector<int> index;index.clear();
    for(int i=0;i+N<=len;i++)
    {
        int times=++mp[hash%10000007];
        if(times>ans){index.clear();index.push_back(i);}
        else if(times==ans){index.push_back(i);}
        ans=max(ans,times);
        if(i+N<len)hash=hash*B+str[i+N]-str[i]*t;
    }
    int ilen=index.size();
    int ansidex=-1;
    for(int k=0;k<ilen;k++)
    {
        int i=index[k];
        if(ansidex==-1||lower(i,ansidex))ansidex=i;
    }
    for(int i=ansidex;i<ansidex+N;i++)cout<<str[i];
    cout<<" "<<ans<<endl;
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值