码迷,mamicode.com
首页 > 其他好文 > 详细

pat 1005 Programming Pattern (35 分)

时间:2019-09-11 12:08:54      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:clu   nat   ott   spl   mina   algo   sample   int   rom   

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 }
View Code

 

pat 1005 Programming Pattern (35 分)

标签:clu   nat   ott   spl   mina   algo   sample   int   rom   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!