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 答案是否 ...
随机推荐
- 自动加载 autoload
自动加载 是什么时候调用的 是实例化某个对象的时候,在当前脚本中没有找到对应类的时候 ,如果当前找到了就不会调用__autoload方法 例如:例子一,找到类 <?php function _ ...
- C# 查看所有的隐藏文件
方法1 通过 位与 static void Main(string[] Args) { //假设扫描C:\Test中—— string[] hiddenfiles = Directory ...
- 安卓备份 To Do(待办事项)的数据库
真正路径:/data/data/com.mediatek.todos/databases/todos.db 使用过链接的路径:/data/user/0/com.mediatek.todos/datab ...
- 学习Css补充知识点
1.text-transform: capitalize;UpperCase等 2.border-size:box,边框在定义的宽高范围内画, content默认,在宽高外画.元互的宽高只指内容 3 ...
- EditPlus 4.3.2583 中文版已经发布
新的版本提升了括号匹配的性能.请点击页面左上角连接下载.
- SpringBoot之RESTFul风格的接口调用(jQuery-Ajax)
一.Get $.ajax({ type: "get", url: "url地址", async: true, dataType:"json" ...
- C++:struct和union 内存字节对齐问题
转自:http://blog.csdn.net/wangyanguiyiyang/article/details/53312049 struct内存对齐问题 1:数据成员对齐规则:结构(struct) ...
- ACM题目————STL + 全排列
今天碰到一个函数,感觉挺好用的,全排列函数 next_permutation! 求全排列的函数,基本上与自己写的DFS时间复杂度差不多,毕竟是标准库.(2018-1-4 添加) 话不多说,直接上题. ...
- poj 1274 The Perfect Stal - 网络流
二分匹配传送门[here] 原题传送门[here] 题意大概说一下,就是有N头牛和M个牛棚,每头牛愿意住在一些牛棚,求最大能够满足多少头牛的要求. 很明显就是一道裸裸的二分图最大匹配,但是为了练练网络 ...
- MVC 学习
基础概念学习:http://www.cnblogs.com/meetyy/p/3451933.html 路由:http://www.cnblogs.com/meetyy/p/3453189.html ...