Codeforces 614E - Necklace
思路:如果奇数超过1个,那么答案是0;否则,所有数的gcd就是答案。
构造方案:每个数都除以gcd,如果奇数个仍旧不超过1个,找奇数个那个在中间(如果没有奇数默认a),其他的平均分到两边。
如果奇数个数超过1个,为了保证中间点之间的每个字母个数是偶数个,那么就拿上种情况的两个构造一段(两个对称拼成一段)。
代码:
#include<bits/stdc++.h>
using namespace std;
int a[];
const int N=2e6;
char s[N];
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n;
cin>>n;
int cnt=;
for(int i=;i<n;i++)
{
cin>>a[i];
if(a[i]&)cnt++;
}
if(n==)
{
cout<<a[]<<endl;
for(int i=;i<a[];i++)
cout<<'a';
cout<<endl;
return ;
}
if(cnt>=)
{
cout<<<<endl;
for(int i=;i<n;i++)
{
for(int j=;j<a[i];j++)cout<<(char)(i+'a');
}
cout<<endl;
return ;
}
int ans=gcd(a[],a[]);
for(int i=;i<n;i++)
ans=gcd(ans,a[i]);
int l=1e6,r=1e6+;
int index=;
for(int i=;i<n;i++)
{
a[i]/=ans;
if(a[i]&)index=i;
}
cnt=;
for(int i=;i<n;i++)
{
if(a[i]&)cnt++;
}
for(int i=;i<a[index];i++)
{
s[r++]='a'+index;
//cout<<s[r-1]<<endl;
}
bool flag=true;
for(int i=;i<n;i++)
{
if(i==index)continue;
while(a[i]!=)
if(flag)
{
s[l--]='a'+i;
a[i]--;
flag=false;
}
else
{
s[r++]='a'+i;
a[i]--;
flag=true;
}
}
string as;
for(int i=l+;i<r;i++)as+=s[i];
if(cnt>=)for(int i=r-;i>l;i--)as+=s[i];
cout<<ans<<endl;
if(cnt>=)ans/=;
for(int i=;i<ans;i++)
cout<<as;
cout<<endl;
return ;
}
Codeforces 614E - Necklace的更多相关文章
- [CodeForces - 614E] E - Necklace
E - Necklace Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic ...
- Codeforces Round #339 (Div. 1) C. Necklace 构造题
C. Necklace 题目连接: http://www.codeforces.com/contest/613/problem/C Description Ivan wants to make a n ...
- Codeforces 526D - Om Nom and Necklace 【KMP】
ZeptoLab Code Rush 2015 D. Om Nom and Necklace [题意] 给出一个字符串s,判断其各个前缀是否是 ABABA…ABA的形式(A和B都可以为空,且A有Q+1 ...
- Codeforces - ZeptoLab Code Rush 2015 - D. Om Nom and Necklace:字符串
D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 【Codeforces 526D】Om Nom and Necklace
Codeforces 526 D 题意:给一个字符串,求每个前缀是否能表示成\(A+B+A+B+\dots+A\)(\(k\)个\(A+B\))的形式. 思路1:求出所有前缀的哈希值,以便求每个子串的 ...
- Codeforces 526D Om Nom and Necklace (KMP)
http://codeforces.com/problemset/problem/526/D 题意 给定一个串 T,对它的每一个前缀能否写成 A+B+A+B+...+B+A+B+A+B+...+B+A ...
- Codeforces 526.D Om Nom and Necklace
D. Om Nom and Necklace time limit per test 1 second memory limit per test 256 megabytes input standa ...
- CodeForces 526D Om Nom and Necklace
洛谷题目页面传送门 & CodeForces题目页面传送门 给定字符串\(a\),求它的每一个前缀,是否能被表示成\(m+1\)个字符串\(A\)和\(m\)个字符串\(B\)交错相连的形式, ...
- Codeforces 1361C - Johnny and Megan's Necklace(欧拉回路)
Codeforces 题目传送门 & 洛谷题目传送门 u1s1 感觉这个题作为 D1C 还是蛮合适的-- 首先不难发现答案不超过 \(20\),所以可以直接暴力枚举答案并 check 答案是否 ...
随机推荐
- 7.MySQL必知必会之用通配符进行过滤-like
用通配符进行过滤-like 1. like操作符 先说两个概念:
- shell篇(一)
login shell与non-login shell: login shell:取得shell时,需要完整的登入流程.如:tty1~tty6登入时,需要输入用户名和密码.此时取得的shell就称为l ...
- nginx之gzlib压缩,expires缓存
gzip压缩网页内容的压缩编码与传输速度优化我们观察news.163.com的头信息请求:Accept-Encoding:gzip,deflate,sdch响应:Content-Encoding:gz ...
- Linux基础命令---bc
bc bc是一种算数语言,其语法和c语言类似,可以交互执行.通过命令行选项可以获得一个标准的数学库.如果请求,在处理任何文件之前定义数学库.BC从处理所有文件的代码开始.命令行中列出的文件按所列顺序排 ...
- c++第十五天
<c++ primer, 5E> 第94页到第99页,笔记: 1.迭代器(iterator):一种比下标访问更通用的访问容器中元素的机制. (并不是所有标准库容器都支持下标访问,<运 ...
- Python3 itchat微信获取好友、公众号、群聊的基础信息
Python3 itchat微信获取好友.公众号.群聊的基础信息 一.简介 安装 itchat pip install itchat 使用个人微信的过程当中主要有三种账号需要获取,分别为: 好友 公众 ...
- SCP报错:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
经过google,出现这个问题的原因是,这是ssh的问题, GkFool大神说(第一次使用SSH连接时,会生成一个认证,储存在客户端的known_hosts中) 我的解决办法是: ssh-keygen ...
- js验证两次输入的密码是否一致
原文链接:http://blog.csdn.net/DDfanL/article/details/51460324
- 20145122《敏捷开发与XP实践 》实验三实验报告
实验名称 敏捷开发与XP实践 实验内容 1.团队代码要使用git在实验楼中托管,要使用结对同学中的一个同学的账号托管. 2.使用git推送代码并对结对同学的代码修改完成后再git推送. 3.掌握重构流 ...
- SPOJ Prime or Not - 快速乘 - 快速幂
Given the number, you are to answer the question: "Is it prime?" Solutions to this problem ...