算法简单,写起来遇到些小问题

 /*
 ID: yingzho1
 LANG: C++
 TASK: contact
 */
 #include <iostream>
 #include <fstream>
 #include <string>
 #include <map>
 #include <vector>
 #include <set>
 #include <algorithm>
 #include <stdio.h>
 #include <queue>
 #include <cstring>
 #include <cmath>
 #include <list>

 using namespace std;

 ifstream fin("contact.in");
 ofstream fout("contact.out");

 int A, B, N;
 string input;

 struct fre {
     int frequency;
     string content;
 };

 //bool cmp(const int &a, const int &b) {return a > b;}
 bool cmp(const fre &a, const fre &b) {
     if (a.frequency == b.frequency) {
         if (a.content.size() == b.content.size()) return a.content < b.content;
         else return a.content.size() < b.content.size();
     }
     else return a.frequency > b.frequency;
 }

 int main()
 {
     fin >> A >> B >> N;
     while (!fin.eof()) {
         string frag;
         getline(fin, frag);
         if (!input.size()) input = frag;
         else input = input + frag;
     }
     map<string, int> rec;
     for (int len = A; len <= B; len++) {
         ; cur + len <= input.size(); cur++) {
             rec[input.substr(cur, len)]++;
         }
     }
     vector<fre> res;
     for (map<string, int>::iterator it = rec.begin(); it != rec.end(); it++) {
         fre tmp;
         tmp.frequency = it->second;
         tmp.content = it->first;
         res.push_back(tmp);
     }
     sort(res.begin(), res.end(), cmp);
     ;
     ;
     ;
     while (step <= N && cur < res.size()) {
         //cout << endl << "step: " << step << endl;
          || res[cur].frequency != res[cur-].frequency) {
             pre = cur;
             if (step == N) break;
             if (cur) fout << endl;
             fout << res[cur].frequency << endl;
             fout << res[cur].content;
             step++;
         }
         else {
              == ) fout << endl << res[cur].content;
             else fout << " " << res[cur].content;
         }
         cur++;
     }
     fout << endl;

     ;
 }

USACO Section 3.1: Contact的更多相关文章

  1. 【USACO 3.1】Contact(01子串按出现次数排序)

    题意:给你一个01字符串,将长度为a到b之间(包含a.b)的子串按照出现次数排序.注意输入输出格式 题解:01子串对应一个二进制,为了区别11和011这样的不同子串,我们把长度也记录下来,官方题解是在 ...

  2. USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)

    usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...

  3. USACO Section 3.3: Riding the Fences

    典型的找欧拉路径的题.先贴下USACO上找欧拉路径的法子: Pick a starting node and recurse on that node. At each step: If the no ...

  4. USACO Section 3.3 Camlot(BFS)

    BFS.先算出棋盘上每个点到各个点knight需要的步数:然后枚举所有点,其中再枚举king是自己到的还是knight带它去的(假如是knight带它的,枚举king周围的2格(网上都这么说,似乎是个 ...

  5. [IOI1996] USACO Section 5.3 Network of Schools(强连通分量)

    nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. --------- ...

  6. USACO Section 5.3 Big Barn(dp)

    USACO前面好像有类似的题目..dp(i,j)=min(dp(i+1,j),dp(i+1,j+1),dp(i,j+1))+1  (坐标(i,j)处无tree;有tree自然dp(i,j)=0) .d ...

  7. USACO Section 1.3 Prime Cryptarithm 解题报告

    题目 题目描述 牛式的定义,我们首先需要看下面这个算式结构: * * * x * * ------- * * * <-- partial product 1 * * * <-- parti ...

  8. USACO Section 1.1 Your Ride Is Here 解题报告

    题目 问题描述 将字符串转变为数字,字母A对应的值为1,依次对应,字母Z对应的值为26.现在有一个字符串,将其中的每个字符转变为数字之后进行累乘,最终的结果对47求余数. 题目给你两个字符串,其中的字 ...

  9. USACO Section 1.1-1 Your Ride Is Here

    USACO 1.1-1 Your Ride Is Here 你的飞碟在这儿 众所周知,在每一个彗星后都有一只UFO.这些UFO时常来收集地球上的忠诚支持者.不幸的是,他们的飞碟每次出行都只能带上一组支 ...

随机推荐

  1. Kibana4学习<一>

    Kibana4 安装方式依然简单,你可以在几分钟内安装好 Kibana 然后开始探索你的 Elasticsearch 索引.只需要预备: Elasticsearch 1.4.4 或者更新的版本 一个现 ...

  2. Java Synchronized Blocks vs. Methods

    It's possible to synchronize both an entire method and a section of code within a method, and you ma ...

  3. Oracle 11g安装与使用

    作为一个新手,学习Oracle,就连安装oracle都感觉到吃力! 经过不间断的搜罗.学习.尝试,找到一些比较有用的“指导”,罗列如下: 1. http://www.2cto.com/database ...

  4. 【bzoj1004】[HNOI2008]Cards

    1004: [HNOI2008]Cards Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2928  Solved: 1754[Submit][Sta ...

  5. JAVA算法系列 快速排序

    java算法系列之排序 手写快排 首先说一下什么是快排,比冒泡效率要高,快排的基本思路是首先找到一个基准元素,比如数组中最左边的那个位置,作为基准元素key,之后在最左边和最右边设立两个哨兵,i 和 ...

  6. topcoder 642

    A:直接拆开字符串写就好了. 今天的题目比较容易一些: B:题目大意: 求最少的翻转次数,每次翻转i是对应 y%i==0都会翻转. 球所有翻转为off状态的最小次数: 从最小idx开始,依次做就好了, ...

  7. (链接保存)CentOS 6.6下yum快速升级内核

    (因Docker建议3.8以上kernel版本)这个方法升级很方便,保存链接备用: http://www.dadclab.com/archives/5340.jiecao Docker Engine安 ...

  8. no module named staticfiles

    原地址:http://blog.sina.com.cn/s/blog_77500e110100umms.html 今天想试一下django的Uploadify,找了个例子,运行时出错:myprojec ...

  9. CSLight研究院之学习笔记结合NGUI(一)

    原地址:http://www.xuanyusong.com/archives/3088 这两天一直在研究CSLight,目前Unity热更新的方式有两种,一种是ulua这个网上的例子已经很多了.还有一 ...

  10. register_globals

    register_globals参数为On的时候很危险 这里记录一下各版本register_globals的情况 PHP5.2版本register_globals默认为On PHP5.3 PHP5.3 ...