zoukankan      html  css  js  c++  java
  • Programming Pattern

    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  . 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
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 typedef unsigned long long ull;
     5 const ull B=100000007;
     6 int N;
     7 string str;
     8 int mp[10000100];
     9 bool lower(int i,int j)
    10 {
    11     for(int t=0;t<N;t++)
    12         if(str[i+t]!=str[j+t])return str[i+t]<str[j+t];
    13     return true;
    14 }
    15 int main()
    16 {
    17     cin>>N;cin.ignore();
    18     getline(cin,str);
    19     ull hash=0;
    20     for(int i=0;i<N;i++)
    21     {
    22         hash=hash*B+str[i];
    23     }
    24     int len=str.length();
    25     ull t=1;
    26     for(int i=0;i<N;i++)t*=B;
    27     int ans=0;vector<int> index;index.clear();
    28     for(int i=0;i+N<=len;i++)
    29     {
    30         int times=++mp[hash%10000007];
    31         if(times>ans){index.clear();index.push_back(i);}
    32         else if(times==ans){index.push_back(i);}
    33         ans=max(ans,times);
    34         if(i+N<len)hash=hash*B+str[i+N]-str[i]*t;
    35     }
    36     int ilen=index.size();
    37     int ansidex=-1;
    38     for(int k=0;k<ilen;k++)
    39     {
    40         int i=index[k];
    41         if(ansidex==-1||lower(i,ansidex))ansidex=i;
    42     }
    43     for(int i=ansidex;i<ansidex+N;i++)cout<<str[i];
    44     cout<<" "<<ans<<endl;
    45     return 0;
    46 }
    诚者,君子之所守也。
  • 相关阅读:
    欧拉计划之题目2:在斐波那契数列中,找出4百万以下的项中值为偶数的项之和。
    MFC非模态对话框的销毁
    MFC:只允许产生一个应用程序实例的具体实现
    从_tiddata看CRT的线程不安全函数
    关于消息循环的深入分析
    MFC:关于MFC窗口对象(CWnd对象)与Window对象(HWND所指对象)的销毁问题
    使用FindFirstFile和FindNextFile对给定目录下所有文件进行广度优先遍历
    工作线程的消息循环与通信
    MFC和设计模式
    _endthreadex与CloseHandle
  • 原文地址:https://www.cnblogs.com/SkystarX/p/12285816.html
Copyright © 2011-2022 走看看