USACO3.1.4 Contact (contact)

略水。
枚举长度l从a到b,找出字符串中所有长度为l的字串然后用map记录,排序输出。
/*
ID:shijiey1
PROG:contact
LANG:C++
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

struct Num {
	string number;
	int cnt;
	bool operator < (const Num &n) const {
		if (cnt != n.cnt) return cnt > n.cnt;
		if (number.size() != n.number.size()) return number.size() < n.number.size();
		int l = number.size();
		for (int i = 0; i < l; i++) {
			if (number[i] != n.number[i]) {
				return number[i] < n.number[i];
			}
		}
	}
};

int a, b, n;
char s[200010];
int lens;
map<string, int> cnt;
vector<Num> ans;
int main() {
	freopen("contact.in", "r", stdin);
	freopen("contact.out", "w", stdout);
	scanf("%d %d %d\n", &a, &b, &n);
	char c;
	while ((c = getchar()) != EOF) {
		if (c == '\n') continue;
		s[lens++] = c;
	}
	for (int l = a; l <= b; l++) {
		for (int i = 0; i <= lens - l; i++) {
			string str = "";
			for (int j = i; j < i + l; j++) {
				str += s[j];
			}
			cnt[str]++;
		}
	}
	map<string, int>::iterator it;
	for (it = cnt.begin(); it != cnt.end(); it++) {
		Num n;
		n.number = it -> first;
		n.cnt = it -> second;
		ans.push_back(n);
	}
	sort(ans.begin(), ans.end());
	int l = 0, r = 0;
	while (n-- && l < ans.size()) {
		r = l;
		if (l) cout << endl;
		while (ans[r].cnt == ans[l].cnt) r++;
		cout << ans[l].cnt << endl;
		int t = 0;
		for (int i = l; i < r; i++) {
			if (i != l) {
				if (!(t % 6)) {
					cout << endl;
				} else {
					cout << ' ';
				}
			}
			cout << ans[i].number;
			t++;
		}
		l = r;
	}
	cout << endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值