A. Prime Permutation

题目连接:

http://www.codeforces.com/contest/123/problem/A

Description

You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1.

Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p ≤ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp × i. If the answer is positive, find one way to rearrange the characters.

Input

The only line contains the initial string s, consisting of small Latin letters (1 ≤ |s| ≤ 1000).

Output

If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO".

Sample Input

abc

Sample Output

YES

abc

Hint

题意

给你一个字符串,然后n是这个字符串的长度。

你可以重排,然后要求满足s[p] = s[ip],p是一个小于等于n的素数。

问你行不行。

题解:

这道题唯一的问题就是,2和3这种素数,他们的公因数是6,他们俩所需要的字母就应该是一样的

数据范围看了一下,才1000,所以直接暴力就好了

其实数据范围是100000也可以做,直接拿个并查集,把一些公因数存在的数直接缩在一起就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1200;
vector<int> p;
int n;
string s;
int vis[maxn];
int ans[maxn];
int num[30];
int getpri()
{
for(int i=2;i<=n;i++)
{
if(vis[i])continue;
p.push_back(i);
for(int j=i;j<=n;j+=i)
vis[j]=1;
}
}
int main()
{
cin>>s;
n=s.size();
getpri();
for(int i=0;i<s.size();i++)
num[s[i]-'a']++;
for(int i=0;i<=n;i++)
ans[i]=-1;
for(int i=0;i<p.size();i++)
{
int k = -1,t = -1;
for(int j=1;j<=n;j++)
if(j%p[i]==0&&ans[j]!=-1)t=ans[j];
if(t==-1)
{
for(int j=0;j<26;j++)
if(num[j]>k)k=num[j],t=j;
}
for(int j=p[i];j<=n;j+=p[i])
{
if(ans[j]!=-1)continue;
if(num[t]==0)return puts("NO"),0;
ans[j]=t;
num[t]--;
}
} for(int i=0;i<26;i++)
if(num[i]>0)ans[1]=i;
cout<<"YES"<<endl;
for(int i=1;i<=n;i++)
printf("%c",ans[i]+'a');
printf("\n");
}

Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力的更多相关文章

  1. Codeforces Beta Round #92 (Div. 2 Only) B. Permutations

    You are given n k-digit integers. You have to rearrange the digits in the integers so that the diffe ...

  2. Codeforces Beta Round #97 (Div. 1) B. Rectangle and Square 暴力

    B. Rectangle and Square 题目连接: http://codeforces.com/contest/135/problem/B Description Little Petya v ...

  3. Codeforces Beta Round #4 (Div. 2 Only) A. Watermelon【暴力/数学/只有偶数才能分解为两个偶数】

    time limit per test 1 second memory limit per test 64 megabytes input standard input output standard ...

  4. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  5. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  6. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  7. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  8. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  9. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

随机推荐

  1. http介绍

    1.http特点: 1>简单快捷: 2>灵活: 3>支持客户端.服务器结构: 4>无连接----无连接的含义是限制每次连接只处理一个请求: 5>无状态----无状态是指协 ...

  2. pycharm显示行号

    在PyCharm 里,显示行号有两种办法: 1,临时设置.右键单击行号处,选择 Show Line Numbers. 但是这种方法,只对一个文件有效,并且,重启PyCharm 后消失. 2,永久设置. ...

  3. python设计模式之常用创建模式总结(二)

    前言 设计模式的创建模式终极目标是如何使用最少量最少需要修改的代码,传递最少的参数,消耗系统最少的资源创建可用的类的实例对象. 系列文章 python设计模式之单例模式(一) python设计模式之常 ...

  4. 55.Jump Game---dp

    题目链接 题目大意:给一个数组,第i个位置的值表示当前可以往前走的最远距离,求从第一个位置能否顺利走到最后一个位置.例子如下: 法一(借鉴):DP,dp[i]表示从上一个位置走到当前位置时,剩余的可以 ...

  5. loadrunner 测试问题汇总

    1.关于Error -27791: Error -27790:Error -27740:        错误如下:        Action.c(198): Error -27791: Server ...

  6. 13.Python3标准库--互联网

    (一)urllib.parse:分解url urllib.parse模块提供了一些函数,可以管理URL以及组成部分 1.解析 from urllib.parse import urlparse ''' ...

  7. linux nginx php-fpm被攻击

    1.nginx错误日志:报错 2018/05/30 16:30:55 [error] 8765#0: *1485 connect() to unix:/tmp/php-70-cgi.sock fail ...

  8. [ python ] 正则表达式及re模块

    正则表达式 正则表达式描述: 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个‘规则字符串’,这个‘规则字符串’用来    表达对字符串的一种过滤 ...

  9. vsftpd.log内容的意义

    vsftpd日志(xferlog格式)的含义 引用: Thu Mar 4 08:12:30 2004 1 202.114.40.242 37 /incoming/index.html a _ o a  ...

  10. Linux 基础——常用的Linux网络命令

    一.学Linux网络命令有什么好处 网络的出现,我们的生活更方便了,处理事情的效率也越来越高,也可以看到全世界文化的差异.同时我们接受新事物的信息越来越来强,新事物的信息也越来越来多.网络对于我们尔等 ...