链接
题目

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".

Examples
Input
abc
Output
YES
abc
Input
abcd
Output
NO
Input
xxxyxxx
Output
YES
xxxxxxy
Note

In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba".

In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4).

In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid.

题目大意

输入一个字符串,判断能否使每一质数和其倍数位上的字符均相同,能输出YES和任一合法解,否则输出NO

分析

一、例如6即使3的倍数又是2的倍数,所以s[3]=s[6],s[2]=s[6],所以这三位要相同。那我们不妨用并查集来维护一组需要全部相同的下标,父亲记作最小的点。要实现这个目的,我们要先处理出全部质数和其倍数,然后判断1到n的所有质数中如果有一质数i乘另一质数j小于n就将j连在i上。经过这一步可以判断出所有要求字符相同的下标位置。

二、下一步就是判断是否合法。我们不妨列几个数(1、2、3、4、5、6、7、8、9、10),我们不难发现不存在次大集合下标数加第三大集合下标数大于最大集合下标数的可能性,所以我们可以放心大胆的将数量最多的那个字母给最大集合中的这些下表用,然后减掉这些数后重新给各个字母的数量排序(字母一共只有26个,所以不用担心超时)。在判断是否合法时顺便将给各集合的字母记录一下,如果中间走不下去了输出NO,否则输出YES然后把之前记录下的字母按下标输出就大功告成了。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int prime[5000],fa[5000],n,t[500000];
char pr[500000];
struct node{
      int t,pl;
}num[50];
struct hhh{
      int t,pl;
}sum[100000];
void init()
{     int i,j,k;
      memset(prime,1,sizeof(prime));
      prime[1]=prime[0]=0;
      fa[1]=1;
      fa[0]=0;
      for(i=2;i<=1100;i++)
         if(prime[i]){
           fa[i]=i;
           for(j=i*2;j<=1100;j+=i){
             prime[j]=0;
             if(!fa[j]);
               fa[j]=i;
           }
         }
}
int sf(int x)
{     return (fa[x]==x?x:fa[x]=sf(fa[x]));
}
void merge()
{     int i,j,k;
      for(i=2;i<=n;i++)
         if(prime[i])
           for(j=i+1;j<=n;j++)
             if(prime[j])
               if(i*j<=n&&fa[i]!=fa[j]){
                   int x,y;
                   x=sf(i);
                   y=sf(j);
                   if(x!=y)
                     fa[y]=x;
               }
}
bool cmp(const node &x,const node &y)
{     return x.t>y.t;
}
bool cmp2(const hhh &x,const hhh &y)
{     return x.t>y.t;
}
int main()
{     int m,i,j,k;
      string s;
      cin>>s;
      n=s.size();
      for(i=0;i<n;i++)
         num[s[i]-'a'+1].t++;
      init();
      merge();
      for(i=1;i<=27;i++)
         num[i].pl=i;
      for(i=1;i<=n;i++)
         sum[sf(i)].t++;
      for(i=1;i<=n;i++)
         sum[i].pl=i;
      sort(sum+1,sum+n+1,cmp2);
      sort(num+1,num+27,cmp);
      k=1;
      while(num[1].t){
          if(!sum[k].t)continue;
          if(num[1].t<sum[k].t){
              puts("NO");
              return 0;
          }
          num[1].t-=sum[k].t;
          t[sum[k].pl]=num[1].pl;
          k++;
          sort(num+1,num+27,cmp);
      }
      puts("YES");
      for(i=1;i<=n;i++)
         pr[i]=t[fa[i]]-1+'a';
      for(i=1;i<=n;i++)
         cout<<pr[i];
      cout<<endl;
      return 0;
}

123 A. Prime Permutation的更多相关文章

  1. Prime Permutation

    Prime Permutation 原题地址: http://codeforces.com/problemset/problem/123/A 题目大意: 给你一个字符串(只包含小写字母),从1开始存放 ...

  2. Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力

    A. Prime Permutation 题目连接: http://www.codeforces.com/contest/123/problem/A Description You are given ...

  3. CodeForces 124C Prime Permutation (数论+贪心)

    题意:给定一个字符串,问你能不能通过重排,使得任意一个素数p <= 字符串长度n,并且 任意的 i <= 长度n/素数p,满足s[p] == s[p*i]. 析:很容易能够看出来,只要是某 ...

  4. C语言程序设计100例之(12):Eratosthenes筛法求质数

    例12   Eratosthenes筛法求质数 问题描述 Eratosthenes筛法的基本思想是:把某范围内的自然数从小到大依次排列好.宣布1不是质数,把它去掉:然后从余下的数中取出最小的数,宣布它 ...

  5. Python学习日记(五)——初识函数(set、深浅拷贝、三目运算、函数、全局变量和局部变量)

    基本数据类型补充 set set集合,是一个无序且不重复的元素集合 #创建 s = {11,22,33,44}#类似字典 s = set() #转换 l = (11,22,33,44) s1 = se ...

  6. Codeforces 1091D New Year and the Permutation Concatenation 找规律,数学 B

    Codeforces 1091D New Year and the Permutation Concatenation https://codeforces.com/contest/1091/prob ...

  7. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  9. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

随机推荐

  1. IdentityServer Topics(4)- 登录

    为了使IdentityServer代表用户发布令牌,该用户必须登录到IdentityServer. Cookie认证 使用来自ASP.NET Core的cookie身份验证处理程序管理的cookie跟 ...

  2. Zabbix实战-简易教程--正则(Regxp)

    一.正则表达式(Regx) 1.概述 正则表达式概念就不解释了.请参考:https://en.wikipedia.org/wiki/Regular_expression#POSIX_extended ...

  3. 关于JS中变量提升的规则和原理的一点理解

        关于变量提升,以前在一些教程和书籍上都听到过,平时开发中也知道有这个规律,但是今天突然在一个公开课中听到时,第一反应时一脸懵逼,然后一百度,瞬间觉得好熟悉啊,差点被这个概念给唬住了,不信我给你 ...

  4. springboot之集成mybatis mongo shiro druid redis jsp

    闲来无事,研究一下spingboot  发现好多地方都不一样了,第一个就是官方默认不支持jsp  于是开始狂找资料  终于让我找到了 首先引入依赖如下: <!-- tomcat的支持.--> ...

  5. Hadoop(十五)MapReduce程序实例

    一.统计好友对数(去重) 1.1.数据准备 joe, jon joe , kia joe, bob joe ,ali kia, joe kia ,jim kia, dee dee ,kia dee, ...

  6. ZOJ 1403&&HDU 1015 Safecracker【暴力】

    Safecracker Time Limit: 2 Seconds      Memory Limit: 65536 KB === Op tech briefing, 2002/11/02 06:42 ...

  7. bzoj 3065: 带插入区间K小值(分块)

    Description 从前有n只跳蚤排成一行做早操,每只跳蚤都有自己的一个弹跳力a[i].跳蚤国王看着这些跳蚤国欣欣向荣的情景,感到非常高兴.这时跳蚤国王决定理性愉悦一下,查询区间k小值.他每次向它 ...

  8. vim与外部文件的粘帖复制

    vim与外部文件的粘帖复制 ubuntu默认vim是不支持从外部文件与vim之间的粘帖复制,vim有自己的剪切版,分别是”0-”9,”-,”8,”+,”:,”/,”%,”i,这些都是vim的寄存器,可 ...

  9. android下载管理、理财、浏览器、商品筛选、录音源码等

    Android精选源码 android仿美拍直播的点赞动画   android视频播放器完美切换全屏.小窗口源码   android类似随手记理财类源码   android简单浏览器源码   andr ...

  10. cesium编程入门(四)界面介绍及小控件隐藏

    感性认识 界面介绍,viewer Geocoder : 查找位置工具,查找到之后会将镜头对准找到的地址,默认使用bing地图 Home Button :视角返回初始位置. Scene Mode Pic ...