pat 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), 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

这题是字符串很常规的题目,可以用后缀数组来做,但是我是用hash硬搞的,使用了自然溢出,我甚至连字典序都没盘,就过了,再次说明pat的数据实在是太水了。  另外,pat的g++编译器不能用gets是什么鬼,求g++的版本???

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<cstdlib>
 7 using namespace std;
 8 #define ui  unsigned long long
 9 int const N=1048576+100;
10 ui  const p=100007;
11 int n;
12 ui h[N],pw[N];
13 char s[N],ans[N];
14 struct node{
15     ui v;
16     int id;
17 }a[N];
18 int cmp(node x,node y){
19     return x.v<y.v;
20 }
21 int main(){
22     scanf("%d",&n);
23     getchar();
24     scanf("%[^\n]",s+1);
25     int len=strlen(s+1);
26     pw[0]=1;
27     for(int i=1;i<=len;i++)
28         pw[i]=pw[i-1]*p;
29     for(int i=1;i<=n;i++)
30         h[n]=h[n]*p+s[i];
31     for(int i=n+1;i<=len;i++){
32         h[i]=(h[i-1]-s[i-n]*pw[n-1])*p+s[i];
33     }
34     for(int i=n;i<=len;i++){
35         a[i].id=i;
36         a[i].v=h[i];
37     }
38     sort(a+n,a+len+1,cmp);
39     int sum=0,cnt=0,x;
40     for(int i=n;i<=len;i++)
41     {
42         if(a[i].v==a[i-1].v) sum++;
43         else sum=1;
44         if(sum>cnt){
45             x=a[i].id;cnt=sum;
46         }
47     }
48     for(int i=x-n+1;i<=x;i++)
49         printf("%c",s[i]);
50     printf(" ");
51     printf("%d\n",cnt);
52     return 0;
53 }

原文地址:https://www.cnblogs.com/ZJXXCN/p/11504938.html

时间: 2024-08-30 02:33:16

pat 1005 Programming Pattern (35 分)的相关文章

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

后缀数组.排序之后得到height数组,然后从上到下将height>=len的都分为一组,然后找到第一组个数最多的输出即可. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #incl

pat考试1002 Business (35 分)

As the manager of your company, you have to carefully consider, for each project, the time taken to finish it, the deadline, and the profit you can gain, in order to decide if your group should take this project. For example, given 3 projects as the

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

7-1 畅通工程之局部最小花费问题(35 分) 某地区经过对城镇交通状况的调查,得到现有城镇间快速道路的统计数据,并提出"畅通工程"的目标:使整个地区任何两个城镇间都可以实现快速交通(但不一定有直接的快速道路相连,只要互相间接通过快速路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修建快速路的费用,以及该道路是否已经修通的状态.现请你编写程序,计算出全地区畅通需要的最低成本. 输入格式: 输入的第一行给出村庄数目N (1≤N≤100):随后的N(N?1)/2行对应村庄间道路的

PAT 1005. Spell It Right

PAT 1005. Spell It Right (20) Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English. Input Specification: Each input file contains one test case. Each case occupies one li

2018/3/19 模拟赛 35分

T1 不会计算几何弃疗了. T2 写了个bitset结果还不如不优化(手动滑稽),因为测样例开小了空间忘了开回去所以0分. 正解是FFT,不会FFT.. T3 暴力35分,正解倍增floyd,学长还讲过但是还是错了,又多了一个要学的知识点. 原文地址:https://www.cnblogs.com/137shoebills/p/8602386.html

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

7-1 畅通工程之局部最小花费问题 (35 分) 某地区经过对城镇交通状况的调查,得到现有城镇间快速道路的统计数据,并提出"畅通工程"的目标:使整个地区任何两个城镇间都可以实现快速交通(但不一定有直接的快速道路相连,只要互相间接通过快速路可达即可).现得到城镇道路统计表,表中列出了任意两城镇间修建快速路的费用,以及该道路是否已经修通的状态.现请你编写程序,计算出全地区畅通需要的最低成本. 输入格式: 输入的第一行给出村庄数目N (1≤N≤100):随后的N(N?1)/2行对应村庄间道路

pat 顶级 1001 Battle Over Cities - Hard Version (35 分)

It is vitally important to have all the cities connected by highways in a war. If a city is conquered by the enemy, all the highways from/toward that city will be closed. To keep the rest of the cities connected, we must repair some highways with the

pat 1003 Universal Travel Sites (35 分)

After finishing her tour around the Earth, CYLL is now planning a universal travel sites development project. After a careful investigation, she has a list of capacities of all the satellite transportation stations in hand. To estimate a budget, she

pat 1035 Password(20 分)

1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) fro