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 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 暴力的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
随机推荐
- .ui/qrc文件自动生成.py文件
前天PL让我们做一个从手机里手机一些数据导出到excel文件里的Tool. 让我们用python去写一个.但是我们都没有学过python..呵呵! 然后昨天看了一些文档.做ui时还需要把图片写入qrc ...
- koa通过get请求获取参数
1.通过get方式请求获取参数的方式有两种 通过上下文获取 通过request获取 获得的格式有两种:query与querystring 注意:querystring为小写,驼峰格式会导致无法获取 2 ...
- perl6 登录phpmyadmin
use HTTP::UserAgent; my $ua = HTTP::UserAgent.new; my $url = 'http://localhost/phpMyAdmin/index.php' ...
- onvif客户端
前言 做开发有8年时间了,ffmpeg和onvif与我是特别有缘的了(说着玩的,我更认为是因为他们确实强大^_^). ffmpeg在毕业设计时就有用到,5年后做windows.linux播放库时又有用 ...
- fastDFS 命令笔记【转】
端口开放 这是命令运行的前提 iptables -I INPUT -p tcp -m state –state NEW -m tcp –dport 22 -j ACCEPT iptables -I I ...
- /proc/cpuinfo 文件分析(查看CPU信息)
/proc/cpuinfo文件分析 根据以下内容,我们则可以很方便的知道当前系统关于CPU.CPU的核数.CPU是否启用超线程等信息. <1>查询系统具有多少个逻辑核:cat /proc/ ...
- C/C++——[05] 函数
函数是 C/C++语言中的一种程序组件单位.一个函数通常代表了一种数据处理的功能,由函数体和函数原型两部分组成.函数原型为这个数据处理功能指定一个标识符号(函数的名称).说明被处理数据的组成及其类型. ...
- Git提交记住用户名和密码
https://www.baidu.com/link?url=R14MHMloypfAfIeiQwCINfY1AZlcoSU7-tYdnqC1PxfmFKs4TWzLOPdtyJbWVfqMqOkRx ...
- java基础8 构造函数和构造代码块
一.构造函数 1 构造函数的作用 给对应的对象进行初始化. 2 构造函数的格式 修饰符 函数名(形式参数){ //函数名就是类名 函数体 } 举例说明: class Perosn{ private i ...
- unittest单元测试
unittest单元测试框架不仅可以适用于单元测试,还可以适用WEB自动化测试用例的开发与执行,该测试框架可组织执行测试用例,并且提供了丰富的断言方法,判断测试用例是否通过,最终生成测试结果.今天笔者 ...