联系 Contact

https://www.luogu.org/problemnew/show/P2724

题意:输出多少频率最多的序列。

题解:

输出

  1. 输出完频率要换行。
  2. 输出6个序列要换行。
  3. 输出完序列要换行。
  4. 如果该频率的序列刚好是6个的话只要换一次行。
  5. 每行第一个序列之前不能有空格。
  6. 每行最后一个序列之后不能有空格。

排序

  1. 频率高的在前。
  2. 频率一样时,位数少的在前。
  3. 位数也一样时,字典序小的在前。

C++版本一

题解:map+vector+sort+枚举

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=200000+10;
const int M=200000;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,sum;
string str,temp;
map<string,int>mp;
vector<string>V[N];
struct node{
    string str;
    int cnt;
    bool operator <(const node &S)const{
        if(cnt==S.cnt){
            if(str.size()==S.str.size()){
                return str<S.str;
            }
            return str.size()<S.str.size();
        }
        return cnt>S.cnt;
    }

}e[N];
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d%d%d",&n,&m,&k);
    while(cin>>temp){
        str+=temp;
    }
    cin>>str;
    for(int i=n;i<=m;i++){
        for(int j=0;i+j<=str.size();j++){
            string temp=str.substr(j,i);
            if(!mp[temp]){
                mp[temp]=++sum;
                e[sum].str=temp;
            }
            int id=mp[temp];
            e[id].cnt++;
        }
    }
    sort(e+1,e+sum+1);
    for(int i=1;i<=sum;i++){
        V[e[i].cnt].push_back(e[i].str);
    }
    for(int i=M;i>0&&cnt<k;i--){
        if(V[i].size()){
            cout<<i<<endl;
            cnt++;
            for(int j=0;j<V[i].size();j++){
                cout<<V[i][j]<<((j+1)%6==0||j==V[i].size()-1?'\n':' ');
            }
        }
    }
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

题解:状态压缩+DP

/*
ID: K wth
PROG: contact
LANG: C++
*/
#include <bits/stdc++.h>
using namespace std;
char s[300000];
int hz [1<<13]; 
int l,r,n,m;
struct note
{
    int x;
    int y;
    int z;
    note(int x=0,int y=0,int z=0):x(x),y(y),z(z) {};
};
vector<note> v;
bool cmp(note x,note y)
{
    return x.y>y.y || x.y==y.y && (x.z<y.z || x.z==y.z && x.x<y.x);
}
void work(int x,int lon)
{
    int mod=(1<<lon)-1;
    while(lon+1)
    {
        putchar('0'+(x>>lon));
        x&=mod;
        mod>>=1;
        lon--;
    }
}
int main()  
{
    scanf("%d %d %d",&l,&r,&n);
    for(int i=0;scanf("\n%s",s+i)!=-1;i+=80) ;
//	printf("%s\n\n",s);return 0;
    r=min(m=strlen(s) , r);
    for(int i=0,zhi=0;i<r;i++)
    {
        zhi=(zhi<<1)|(s[i]-'0');
        if(i>=l-1)
        {
            int mod=(1<<(i+1))-1,z=zhi;
            memset(hz,0,sizeof(hz));
            hz[z]=1;
            for(int j=i+1;j<m;j++)
            {
                z=((z<<1)|(s[j]-'0'))&mod;
                hz[z]++;
            }
            for(int j=0;j<=mod;j++)
                if(hz[j])
                    v.push_back(note(j,hz[j],i));
        }
    }
    sort(v.begin(),v.end(),cmp);
    for(int i=0,last=1<<18,j=0,have=0;i<v.size();i++,have++)
    {
    //	cout<<"test:"<<v[i].x<<v[i].y<<v[i].z<<endl;
        if(v[i].y<last)
        {
            last=v[i].y;
            if(j++==n || !last) break;
            if(i) putchar('\n');
            have=0;
            printf("%d\n",last);
        }
        else if(have==6) putchar('\n'),have=0;
        else putchar(' ');
        work(v[i].x,v[i].z);
    }
    putchar('\n');
}

 

全部评论

相关推荐

码农索隆:有点耳熟,你们是我教过最差的一届
点赞 评论 收藏
分享
07-05 16:23
门头沟学院 Java
mengnankk:我投了300,约了5 6个面试。感觉项目写的太多了。一个项目就写五六个亮点,不是把整个项目的功能描述下。其他的没啥,简历看起来有点长
点赞 评论 收藏
分享
06-12 16:00
天津大学 Java
牛客30236098...:腾讯坏事做尽,终面挂是最破防的 上次被挂了后我连简历都不刷了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 12:31
以前小时候我最痛恨出轨、偷情的人,无论男女,为什么会出轨?现在我成了自己最讨厌的人,没想到分享的东西在牛客会被这么多人看,大家的评价都很中肯,我也认同,想过一一回复,但我还是收声了,我想我应该说说这件事,这件事一直压在我心里,是个很大的心结,上面说了人为什么出轨,我大概能明白了。我们大一下半年开始恋爱,开始恋爱,我给出了我铭记3年的承诺,我对她好一辈子,我永远不会背叛,我责任心太重,我觉得跟了我,我就要照顾她一辈子,我们在一起3年我都没有碰过她,她说往东我就往东,她说什么我做什么,她要我干什么,我就干什么!在学校很美好,中途也出过一些小插曲,比如男闺蜜、男闺蜜2号等等等。但我都强迫她改掉了,我...
牛客刘北:两个缺爱的人是没有办法好好在一起的,但世界上哪有什么是非对错?你后悔你们在一起了,但是刚刚在一起的美好也是真的呀,因为其他人的出现,你开始想要了最开始的自己,你的确对不起自己,21岁的你望高物远,你完全可以不谈恋爱,去过你想要的生活,你向往自由,在一起之后,你要想的不是一个人,而是两个人,你不是变心了,就像你说的,你受够了,你不想包容了,冷静几天是你最优的选择,爱人先爱己。
社会教会你的第一课
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务