codeforces--123A--思维题


题目大意:

给出一个字符串,问能否通过打乱它的顺序得到一个新的字符串,这个字符串满足所有的质数位和位数是这个质数的倍数的位数上的字母相同,输出能否,如果能,构造一组解.


题目分析:


首先我们知道输入的字符串的字母的顺序无所谓,我们只需要记录每个字母出现的次数。
然后我们发现不管n是多少,集合的形式一定是一个大集合,和一些规模为1的小集合,且这些集合的元素是质数,且大集合不存在这个质数的倍数。这个很显然。
利用上面的结论,我们只需要找到大集合的规模,然后看是否存在一个字母的数量不小于这个大集合的规模,如果存在,那么显然有解。否则无解。
构造最终的解,只需要用找出的字母构造大集合中每个位置的字母,其他的随意即可。 

 

 


注意:mark中1表示的是最大集合元素,0的个数也就是cnt的大小

 

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
bool isprime[1010];
bool mark[1010];
int num[27];
string s;
//筛选质数 
void init(){
	memset(isprime,true,sizeof(isprime));
	for(int i=2;i<=1000;i++){
		if(isprime[i]){
			for(int j=i<<1;j<=1000;j+=i){
				isprime[j]=false;
			}
		}
	}
}

int main(){
	init();
	while(cin>>s){
		int len=s.size();
		if(len==1){
			cout<<"YES"<<endl;
			cout<<s<<endl;
			continue;
		}
		//mark数组标记出来的元素属于最大集合的元素 
		memset(mark,false,sizeof(mark));
		int cnt=1;
		//cnt表示除了最大的集合,还有多少集合 
		for(int i=len;i>=2;i--){
			//下面表示没有标记的都是当前还没有标记的质
			//所以除了最大的集合,其他集合都只有一个元素(一个质数下标) //关键 
			if(isprime[i]&&!mark[i])cnt++;
			else//否则就是最大集合的元素 
				if(!isprime[i]){
					mark[i]=true;//标记 
					for(int j=2;j*j<=i;j++){
						if(i%j)continue;
						mark[j]=mark[i/j]=true;//标记 
					}
				}
		}
		memset(num,0,sizeof(num));//记录每个字母出现的次数 
		for(int i=0;i<len;i++){
			num[s[i]-'a']++;
		}
		int id=-1;
		//接下来找有没有不小于最大集合元素个数的字母
		//如果找到,就把此字母作为最大集合中的下标元素所对应的字符串中的字母
		//len-cnt就是最大集合的元素个数(想想cnt的含义并结合那个关键) 
		for(int i=0;i<len;i++){
			if(len-cnt<=num[s[i]-'a']){
				id=s[i]-'a';
				num[s[i]-'a']-=(len-cnt);//先减掉(len-cnt)个 
				break;
			}
		}
		if(id==-1){//没找到,去合法方案 
			cout<<"NO"<<endl;
			continue;
		}
		for(int i=0;i<len;i++){
			//mark[i+1]==true,说明此下标在最大集合中 
			if(mark[i+1])s[i]=char(id+'a');
			else for(int j=0;j<26;j++){
				//不在最大集合中的下标对应着随便一个存在的字母
				//注意,j==id也不没问题,因为已经提前把(len-cnt)个id拿出来了 
				if(num[j]){
					num[j]--;
					s[i]=char(j+'a');
					break;
				}
			}
		}
		cout<<"YES"<<endl;
		cout<<s<<endl;
	}
	return 0;
}
//            /\       |  /  |**、
//			 /  \      | /   |   \
//			/    \     |/    |   /  _____                      ____   |  /
//		   /------\    |\    |__/  /     \  \      /\      /  /    \  | /
//		  /        \   | \   |    /       \  \    /  \    /  /______\ |/
//		 /          \  |  \  |    \       /   \  /    \  /   \        |
//      /            \ |   \ |     \_____/     \/      \/     \_____  |
/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值