Hackerrank:Yet Another KMP Problem
来源:互联网 发布:java中的设计模式 编辑:程序博客网 时间:2024/05/17 01:32
Yet Another KMP Problem
This challenge uses the famous KMP algorithm. It isn't really important to understand how KMP works, but you should understand what it calculates.
A KMP algorithm takes a string,
Here is an implementation example of KMP:
kmp[1] = 0;for (i = 2; i <= N; i = i + 1){ l = kmp[i - 1]; while (l > 0 && S[i] != S[l + 1]){ l = kmp[l]; } if (S[i] == S[l + 1]){ kmp[i] = l + 1; } else{ kmp[i] = 0; }}
Given a sequence
- The frequency of letter '
a ' inS is exactlyx1 , the frequency of letter 'b ' inS is exactlyx2 , and so on. - Let's assume characters of
S are numbered from1 toN , where∑i=1nxi=N . We apply the KMP algorithm toS and get a table,kmp , of sizeN . You must ensure that the sum ofkmp[i] for alli is minimal.
If there are multiple strings which fulfill the above conditions, print the lexicographically smallest one.
Input Format
A single line containing
Constraints
- The sum of all
xi will be a positive integer≤106 .
Output Format
Print a single string denoting
Sample Input
2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Sample Output
aabb
Explanation
The output string must have two '
The minimum sum is
题意是给出一个字符串各个字符的出现次数,要使得一个,这个字符的kmp数组的和最小,还要字典序最大。
kmp数组的和最小,其实就是前缀不想等,这样就要把出现次数最少的那个字符放到第一个,然后考虑各种情况,如果这个字符字典序在后面,那直接按字典序排好,输出。
如果这个字符本身在字典序第一位,那么考虑让这个字符与其他字符交叉输出使得前缀字符不相等。
代码:
#pragma warning(disable:4996)#include <iostream>#include <functional>#include <algorithm>#include <cstring>#include <vector>#include <string>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <deque>#include <set>#include <map>using namespace std;typedef long long ll;#define INF 0x3fffffff#define repp(i, n, m) for (int i = n; i <= m; i++)#define rep(i, n, m) for (int i = n; i < m; i++)#define sa(n) scanf("%d", &(n))#define mp make_pair#define ff first#define ss second#define pb push_backconst int maxn = 1e6 + 5;const ll mod = 1000000;const double PI = acos(-1.0);vector< pair <int, char> > v;bool cmp(const pair<int, char>&a, const pair<int, char>&b){return a.ss < b.ss;}void solve(){int i, j, k;rep(i, 0, 26){sa(k);if (k == 0)continue;v.pb(mp(k, i + 'a'));}sort(v.begin(), v.end());sort(v.begin() + 1, v.end(), cmp);if (v.size() == 1){rep(i, 0, v[0].ff){printf("%c", v[0].ss);}printf("\n");return;}int le = 0, ri = 1;//buf[cur++] = v[le].ss, v[le].ff--;printf("%c", v[le].ss);v[le].ff--;if (v[le].ss > v[ri].ss){sort(v.begin(), v.end(), cmp);rep(i, 0, v.size()){rep(j, 0, v[i].ff){printf("%c", v[i].ss);}}printf("\n");}else{while (ri < v.size()){if (v[le].ff){v[le].ff--;printf("%c", v[le].ss);}if (v[ri].ff){v[ri].ff--;printf("%c", v[ri].ss);if (v[ri].ff == 0){ri++;}}}}}int main(){#ifndef ONLINE_JUDGE freopen("i.txt", "r", stdin);freopen("o.txt", "w", stdout);#endifsolve();return 0;}
- Hackerrank:Yet Another KMP Problem
- Yet Another Multiple Problem
- hdu4474-Yet Another Multiple Problem
- hdu4474 Yet Another Multiple Problem
- HDU4474 Yet Another Multiple Problem
- Yet Another Multiple Problem-----BFS
- [codeforces903G]Yet Another Maxflow Problem
- spoj 1771 Yet Another N-Queen Problem
- Yet Another Multiple Problem (hdu 4474)
- HDU 4474 Yet Another Multiple Problem
- hdu 4474 Yet Another Multiple Problem bfs
- HDU 4474 Yet Another Multiple Problem
- HDU 4474 Yet Another Multiple Problem 搜索
- 【搜索】 HDOJ 4474 Yet Another Multiple Problem
- HDOJ 4474 Yet Another Multiple Problem
- HDU 4474 Yet Another Multiple Problem
- hdu 4474 Yet Another Multiple Problem (bfs)
- hdu 4474 Yet Another Multiple Problem(BFS)
- Java千百问_06数据结构(004)_java数据类型的字面值是什么
- A Tour of Golang (二)
- IMF传奇行动第85课:Spark Streaming第四课:基于HDFS的Spark Streaming案例实战和内幕源码解密
- eclipse luna4.4配置beans.xml和hibernate.cfg.xml提示类的包路径信息
- Java Socket应用
- Hackerrank:Yet Another KMP Problem
- 简单的Kafka0.8消费者程序
- poj 1163 The Triangle --dp
- 延云YDB&&YA100安装部署文档
- Android源码解析——AsyncTask
- 人生不设限 没有不可能
- POJ 1101(BFS)
- 【Linux学习笔记五】用户与用户组管理
- 20160501