codeforces 123A/124C Prime Permutation
来源:互联网 发布:windows常用oid 编辑:程序博客网 时间:2024/05/18 02:08
挺有意思的一道水题。
题目传送门
题目大意:一个字符串(下标从1开始),设其总长为L,问能否改变字符的顺序,使得所有不超过L的质数p,满足s[p]=s[p*k],p*k<=L。
分析:注意到2是质数(关键),因此对于所有偶数下标所对应的字符一定相同。再有对于下标为p的字符,若p*2<L,则s[p*2]与下标为2的字符相同,p也自然与s[2]相同。
即除了第1个位置和满足p*2>L的位置的字符可以任意取,其余的位置的字符都是相同的。
故只需统计一下字符串中出现次数最多的字符,设为mx ;然后统计一下p*2>L的位置个数,设为m。若L-m<=mx则必然可以通过交换字符顺序满足条件。
#include<bits/stdc++.h>using namespace std;bool v[1005];int pos[1005],prim[505],tot=0,cnt[26];void init(int n){ memset(v,0,sizeof(v)); for(int i=2;i<=n;++i) { if(!v[i]) prim[tot++]=i; for(int j=0;j<tot&&prim[j]*i<=n;++j) { v[i*prim[j]]=1; if(i%prim[j]==0) break; } }}char s[1005],C,ans[1005];int main(){ int i,l,j,m,mx=-1; scanf("%s",s+1); memset(ans,0,sizeof(ans)); l=strlen(s+1); init(l); for(i=1;i<=l;++i){ ++cnt[s[i]-'a']; if(cnt[s[i]-'a']>mx){ mx=cnt[s[i]-'a']; C=s[i]-'a'; } } m=0,pos[++m]=1; for(i=0;i<tot;++i) if(prim[i]*2>l) pos[++m]=prim[i]; if(l-m>mx) puts("NO"); else{ puts("YES"); for(i=1;i<=m;++i){ for(j=0;j<26;++j) if((cnt[j]&&j!=C)||(j==C&&cnt[j]>l-m)){ --cnt[j];ans[pos[i]]=j+'a'; break; } } for(i=1;i<=l;++i) if(!ans[i]) ans[i]=C+'a'; puts(ans+1); } return 0;}
0 0
- codeforces 123A/124C Prime Permutation
- Codeforces 123A. Prime Permutation
- codeforces 123A A. Prime Permutation(数论+构造)
- codeforces C. Diverse Permutation
- 【CODEFORCES】C. Diverse Permutation
- 暑假第一周 C . CodeForces 660A Co-prime Array
- Codeforces-233A Perfect Permutation
- CodeForces 482A Diverse Permutation
- CodeForces 482A Diverse Permutation
- CodeForces 233A Perfect Permutation
- codeforces 285C - Building Permutation
- codeforces 285C - Building Permutation
- codeforces 483C.Diverse Permutation
- CodeForces-483C Diverse Permutation
- CodeForces 483C Diverse Permutation
- Codeforces-285C-Building Permutation
- codeforces 287 C. Lucky Permutation
- codeforces 432c Prime Swaps
- R语言常用函数集合
- KG 3.2.0_C SVN 修改情况
- maven 配置与常用命令
- 自定义类型变量的大小(数据对齐)
- boost自带的单例模式不能跨dll使用
- codeforces 123A/124C Prime Permutation
- Android gradle 编译 MultiDex
- Mysql load data 命令解析、处理 error 29 (ErrCode: 13) 错误(在ubuntu环境下)
- 2015年2月到2015年6月8日(2.0发布)Swift主要更新
- R语言与统计笔记
- win7 xshell登录虚拟机的centos
- android基础学习之dispatchKeyEvent
- ACM训练方案-POJ题目分类
- CONST