CodeForce 724D. Dense Subsequence bY Assassin
2016-10-09 21:42
253 查看
D. Dense Subsequence time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output You are given a string s, consisting of lowercase English letters, and the integer m. One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves. Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order. Formally, we choose a subsequence of indices 1 ≤ i1 < i2 < ... < it ≤ |s|. The selected sequence must meet the following condition: for every j such that 1 ≤ j ≤ |s| - m + 1, there must be at least one selected index that belongs to the segment [j, j + m - 1], i.e. there should exist a k from 1 to t, such that j ≤ ik ≤ j + m - 1. Then we take any permutation p of the selected indices and form a new string sip1sip2... sipt. Find the lexicographically smallest string, that can be obtained using this procedure. Input The first line of the input contains a single integer m (1 ≤ m ≤ 100 000). The second line contains the string s consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100 000. It is also guaranteed that the number m doesn't exceed the length of the string s. Output Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above. Examples Input 3 cbabc Output a Input 2 abcab Output aab Input 3 bcabcbaccba Output aaabb
这题超级简单你敢信?就是个贪心。。。每次从头开始,然后每次从m个中选出最小的,下一次从上次的最小开始,直到结束,每次记录一下次数,吧用到的最大的字母挑出来,小于他的原字符串有多少输出多少,最大的那个用到多少输出多少。。。真蠢这题错了10次?还不会看了别人的代码。。。
#include<bits/stdc++.h> #define input freopen("input.txt","r",stdin) using namespace std; char a[100010]; int c[26]; int use[26]; int m; int main() { input; int i,j; string s; while(scanf("%d",&m)!=EOF) { memset(c,0,sizeof(c)); memset(use,0,sizeof(use)); cin>>s; for(i=0;i<s.length();i++) { a[i+1]=s[i]; c[s[i]-'a']++; } int len=s.size(); int pos=0,p,maxx,minn; for(maxx=0,pos=0;pos+m<=len;) { minn=26; p=pos; for(i=1;i<=m;i++) { if(a[pos+i]-'a'<=minn) { minn=a[pos+i]-'a'; p=i+pos; } } use[a[p]-'a']++; if(maxx<a[p]-'a') { maxx=a[p]-'a'; } pos=p; } for(i=0;i<maxx;i++) for(j=1;j<=c[i];j++) printf("%c",i+'a'); for(i=1;i<=use[maxx];i++)printf("%c",maxx+'a'); cout<<endl; } return 0; }
膜拜zchahaha
相关文章推荐
- Codeforce-242E-XOR on Segment(线段树区间更新)
- codeforce 510C Fox And Names (拓扑排序)
- codeforce #500场
- codeforce 5B Center Alignment
- Codeforce 546 A. Soldier and Bananas
- codeforce 298 A Snow Footprints
- codeforce #387 D. Winter Is Coming
- codeforce 17 B
- Codeforce 题目118D. Caesar's Legions(DP背包)
- codeforce 825E. Minimal Labels 拓扑排序 贪心思想
- CodeForce 448C 木片填涂问题
- codeforce 399 div1 a
- 解决--No instance field forceOldAnimationCode of type Z in class ImageDecodeOptions
- Codeforce 621(A,B,C,D,E)
- Codeforce - 204B - Little Elephant and Cards
- Codeforce - 366B - Dima and To-do List
- codeforce 837B Flag of Berland(矩形判断)
- codeforce GYM 100741 A Queries
- code force 401B. Game of Credit Cards
- CodeForce 396A On Number of Decompositions into Multipliers