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;
}

PAT TOP 1005 Programming Pattern (35 分)哈希做法的更多相关文章

  1. PAT (Top Level) Practise 1005 Programming Pattern (35)

    后缀数组.排序之后得到height数组,然后从上到下将height>=len的都分为一组,然后找到第一组个数最多的输出即可. #pragma comment(linker, "/STA ...

  2. PAT (Top Level) Practise 1008 Airline Routes(Tarjan模版题)

    1008. Airline Routes (35) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Given a ...

  3. CCF(317号子任务)-35分:Dijikstra算法

    317号子任务 201903-5 为了过前60分,想使用dijikstra优化算法的,但是最后还是只过了35分.这里的思路只需要先将所有的行星据点进行一次dijikstra,分别存储所有点到行星的最短 ...

  4. Top 10 Programming Fonts

    Top 10 Programming Fonts Sunday, 17 May 2009 • Permalink Update: This post was written back in 2009, ...

  5. PTA 7-1 畅通工程之局部最小花费问题(35 分)

    7-1 畅通工程之局部最小花费问题(35 分) 某地区经过对城镇交通状况的调查,得到现有城镇间快速道路的统计数据,并提出“畅通工程”的目标:使整个地区任何两个城镇间都可以实现快速交通(但不一定有直接的 ...

  6. PAT 乙级 1005

    题目 题目地址:PAT 乙级 1005 题解 本题主要就在于将一个数的一系列计算结果不重复地存储起来并便于检索,考虑到STL中的集合有相似的特性,使用set可以有效地简化代码和运算. 过程如下: (初 ...

  7. PAT乙级:1088 三人行 (20分)

    PAT乙级:1088 三人行 (20分) 题干 子曰:"三人行,必有我师焉.择其善者而从之,其不善者而改之." 本题给定甲.乙.丙三个人的能力值关系为:甲的能力值确定是 2 位正整 ...

  8. PAT乙级:1064 朋友数 (20分)

    PAT乙级:1064 朋友数 (20分) 题干 如果两个整数各位数字的和是一样的,则被称为是"朋友数",而那个公共的和就是它们的"朋友证号".例如 123 和 ...

  9. PTA 7-4 最小生成树的唯一性 (35分)

    PTA 7-4 最小生成树的唯一性 (35分) 给定一个带权无向图,如果是连通图,则至少存在一棵最小生成树,有时最小生成树并不唯一.本题就要求你计算最小生成树的总权重,并且判断其是否唯一. 输入格式: ...

随机推荐

  1. JAVA基础_修饰符

    引言:Java的修饰符根据修饰的对象不同,分为类修饰符.方法修饰符.变量修饰符,其中每种修饰符又分为访问控制修饰符和非访问控制修饰符.访问控制存在的原因:a.让客户端程序员无法触及他们不应该触及的部分 ...

  2. mybatis执行DDL语句

    对MyBatis一直停留在仅仅会用的阶段,常用的场景就是通过MyBatis对表数据进行DML(insert, delete, update等)操作,从来没有想过通过MyBatis对数据库进行DDL(c ...

  3. [The 2019 Asia Yinchuan First Round Online Programming] D Take Your Seat

    也许更好的阅读体验 \(\mathcal{Description}\) 原题链接 题目大意 该题目有两个问题 \(Task\ 1\),有\(n\)个人\(n\)个座位,每个人都有一个对应的座位,每个人 ...

  4. MSMQ消息队列,包括远程访问

    之前的项目用到了队列,现在总结一下,下面有非常详细的DEMO,希望能对有需要的人提供帮助. 使用场景:在项目中,将一些无需即时返回且耗时的操作提取出来,进行了异步处理,而这种异步处理的方式大大的节省了 ...

  5. .netCore 简易Web 项目

    static async Task Main(string[] args) { var _httpListener = new HttpListener(); _httpListener.Prefix ...

  6. C#数字日期转成中文日期

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  7. VBA switch语句

    当用户想要根据Expression的值执行一组语句时,使用Switch Case语句. 每个值被称为一个”情况”,并根据每种情况变量接通测试.如果测试表达式与用户指定的任何Case不匹配,则执行Cas ...

  8. 【转载】C#使用Except方法求取两个List集合的差集数据

    在C#语言的编程开发中,针对List集合的运算有时候需要计算两个List集合的差集数据,集合的差集是取在该集合中而不在另一集合中的所有的项.A集合针对B集合的差集数据指的是所有在A集合但不在B集合的元 ...

  9. 关于Echarts柱状图点击事件的实现方法

    开发过程中,我们经常会碰到这样的需求:在柱状图上,点击某条柱形,调用相应的方法或跳转相应的界面 接下来就详细介绍如何实现柱状图的点击事件,其中maChart是绘图对象 一.简单的点击事件 myChar ...

  10. Excel导入+写入数据库

    1.引用服务 2.前端 <h2>这里是上传Excel功能页面</h2> <div> <form action="/Improve_Excel/get ...