zoukankan      html  css  js  c++  java
  • Prime Permutation(思维好题 )

     Prime Permutation

    codeforces 123A

    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.

     题意:长度为n的字符串s,问是否能将s重新排列
    满足对任意一个素数p 都有 s[p]=s[p*i] [i=1,2..n/p]  n<=1000.通俗一点,就是2 3 4 6...这些位置上的字母必须一样。我们发现:如果一个质数*2还小于等于给定字符串的总长度,那么这个质数的倍数和比它小的质数和它们的倍数的位置上的字母是一样的。

    质数p,q满足p*q<=n 那么p*2,q*2<=n
    若质数p*2>n 那么位置p可以放任意一个字符.
    若质数p*2<=n 那么这些位置都会等于同一个字符. 

    所以标记这些放相同字符的位置 然后看出现次数最多的字符能否放入即可.

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <queue>
      5 #include <vector>
      6 #include <algorithm>
      7 using namespace std;
      8 const int maxn=1010;
      9 char s[maxn];
     10 int flag;
     11 vector<int>v;
     12 char ans[maxn];
     13 int cnt[maxn],prime[maxn];
     14 struct node{
     15     int zimu;
     16     int num;
     17     friend bool operator < (node a,node b)
     18     {
     19         return a.num<b.num;
     20     }
     21 };
     22 void init()
     23 {
     24     for(int i=2;i<maxn;i++)
     25     {
     26         if(!prime[i])
     27         {
     28             v.push_back(i);
     29             for(int j=i+i;j<maxn;j+=i)
     30             {
     31                 prime[j]=1;
     32             }
     33         }
     34     }
     35 }
     36 int main()
     37 {
     38     scanf("%s",s+1);
     39     int len=strlen(s+1);
     40     init();
     41     memset(prime,0,sizeof(prime));
     42     for(int i=0;i<v.size();i++)
     43     {
     44         if(v[i]*2>len)
     45             break;
     46         for(int j=1;j*v[i]<=len;j++)
     47         {
     48             prime[j*v[i]]=1;
     49         }
     50     }
     51     int tot=0;
     52     for(int i=1;i<=len;i++)
     53     {
     54         cnt[s[i]-'a']++;
     55         if(prime[i])
     56         {
     57             tot++;
     58         }
     59     }
     60     priority_queue<node>q;
     61     for(int i=0;i<26;i++)
     62     {
     63             node x;
     64             x.num=cnt[i];
     65             x.zimu=i;
     66             q.push(x);
     67     }
     68     node head=q.top();
     69     if(head.num<tot)
     70     {
     71         puts("NO");
     72         return 0;
     73     }
     74     puts("YES");
     75     int res=0;
     76     for(int i=1;i<=len;i++)
     77     {
     78         if(prime[i])
     79         {
     80             res++;
     81             ans[i]=q.top().zimu+'a';
     82          
     83         }
     84     }
     85     head=q.top();
     86     q.pop();
     87     head.num-=res;
     88     if(head.num)
     89     {
     90         q.push(head);
     91     }
     92     for(int i=1;i<=len;i++)
     93     {
     94         if(!prime[i])
     95         {
     96             head=q.top();
     97             ans[i]=head.zimu+'a';
     98             head.num--;
     99             q.pop();
    100             if(head.num)
    101                 q.push(head);
    102         }
    103     }
    104     for(int i=1;i<=len;i++)
    105         printf("%c",ans[i]);
    106     printf("
    ");
    107 }
  • 相关阅读:
    python 获取当前路径
    python 执行结果输出为txt
    python def 定义函数
    python:浅析python 中__name__ = ‘__main__’ 的作用
    python 数组新增或删除元素
    python在Windows安装PIL的方法
    静态路由学习总结
    zookeeper客户端命令行操作
    kudu安装过程中可能出现的问题
    sudo: /etc/sudoers is world writable|给用户添加权限报错
  • 原文地址:https://www.cnblogs.com/1013star/p/9751956.html
Copyright © 2011-2022 走看看