思路:

a素数->b合数

c素数->b合数

a,c属于一类

so,预处理相同的,并且计数。1000怎么搞都无压力;

我这里也预处理了字母个数,从集合大的枚举下来,每次拿字母个数最多的去匹配。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4.  
  5. const int N=1e3+10;
  6.  
  7. bool isprime(int x)
  8. {
  9. if(x==1) return false;
  10. int q=sqrt(x);
  11. for(int i=2; i<=q; i++)
  12. if(x%i==0) return false;
  13. return true;
  14. }
  15.  
  16. pair<int,int>sum[1010];
  17. int ssum[30];
  18.  
  19. char s[N],ans[N];
  20. bool vis[N];
  21. int n;
  22. vector<int>prime;
  23. vector<int>num[200];
  24.  
  25. void init()
  26. {
  27. for(int i=1; i<=1000; i++)
  28. if(isprime(i)) prime.push_back(i);
  29. }
  30.  
  31. int pre[N];
  32. int Find(int x)
  33. {
  34. int r=x;
  35. while(pre[r]!=r)
  36. r=pre[r];
  37. int i=x,j;
  38. while(pre[i]!=r)
  39. {
  40. j=pre[i];
  41. pre[i]=r;
  42. i=j;
  43. }
  44. return r;
  45. }
  46.  
  47. pair<int,int>xs[1010];
  48. vector<int>pp[1010];
  49.  
  50. int main()
  51. {
  52. //预处理素数
  53. init();
  54.  
  55. scanf("%s",s+1);
  56. n=strlen(s+1);
  57.  
  58. //求和
  59. for(int i=1; i<=n; i++)
  60. {
  61. int x=s[i]-'a';
  62. ssum[x]++;
  63. }
  64. for(int i=0; i<26; i++)
  65. {
  66. sum[i].first=ssum[i];
  67. sum[i].second=i;
  68. }
  69.  
  70. //分块。
  71. int sz=prime.size();
  72. int ssz=sz;
  73. for(int i=0; i<sz; i++)
  74. {
  75. if(prime[i]>n)
  76. {
  77. ssz=i;
  78. break;
  79. }
  80. for(int k=1; k<=n; k++)
  81. {
  82. if(k*prime[i]>n) break;
  83. num[i].push_back(k*prime[i]);
  84. }
  85. }
  86. for(int i=1; i<=n; i++)
  87. pre[i]=i;
  88. for(int i=0; i<ssz; i++)
  89. {
  90. int u=prime[i];
  91. int sss=num[i].size();
  92. for(int k=0; k<sss; k++)
  93. {
  94. int v=num[i][k];
  95. int uu=Find(u);
  96. int vv=Find(v);
  97. if(uu!=vv)
  98. pre[uu]=vv;
  99. }
  100. }
  101. //建立 集合个数 和 集合元素
  102. for(int i=1;i<=n;i++)
  103. xs[i].first=0;
  104. for(int i=1; i<=n; i++)
  105. {
  106. int x=Find(i);
  107. xs[x].first++;
  108. xs[x].second=x;
  109. pp[x].push_back(i);
  110. }
  111.  
  112. //从大到小
  113. sort(xs+1,xs+n+1);
  114. sort(sum,sum+26);
  115. // for(int i=n;i>=1;i--)
  116. // {
  117. // printf("%d %d\n",xs[i].first,xs[i].second);
  118. // }
  119. // for(int j=25;j>=23;j--)
  120. // {
  121. // printf("%d\n",sum[j].first);
  122. // }
  123. for(int i=n; i>=1; i--)
  124. {
  125. int sz=xs[i].first; //集合个数
  126. int x=xs[i].second; //集合老大
  127. if(pp[x].size()==0) break;
  128. bool flag=false;
  129. int j=25;
  130. if(sum[j].first>=sz)
  131. {
  132. for(int k=0; k<sz; k++)
  133. ans[pp[x][k]-1]=sum[j].second+'a';
  134. sum[j].first-=sz;
  135. }
  136. else
  137. flag=true;
  138. sort(sum,sum+26);
  139. if(flag)
  140. {
  141. puts("NO");
  142. return 0;
  143. }
  144. }
  145. ans[n]='\0';
  146. puts("YES");
  147. printf("%s\n",ans);
  148. return 0;
  149. }

CodeForces 124C【连通块】的更多相关文章

  1. Codeforces Round #375 (Div. 2)——D. Lakes in Berland(DFS连通块)

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

    C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. C. Edgy Trees Codeforces Round #548 (Div. 2) 【连通块】

    一.题面 here 二.分析 这题刚开始没读懂题意,后来明白了,原来就是一个数连通块里点数的问题.首先在建图的时候,只考虑红色路径上的点.为什么呢,因为为了不走红色的快,那么我们可以反着想只走红色的路 ...

  4. CodeForces 690D1 The Wall (easy) (判断连通块的数量)

    题意:给定一个图,问你有几个连通块. 析:不用说了,最简单的DFS. 代码如下: #include <bits/stdc++.h> using namespace std; const i ...

  5. Codeforces 920E Connected Components? 补图连通块个数

    题目链接 题意 对给定的一张图,求其补图的联通块个数及大小. 思路 参考 ww140142. 维护一个链表,里面存放未归入到任何一个连通块中的点,即有必要从其开始进行拓展的点. 对于每个这样的点,从它 ...

  6. Codeforces 990 调和级数路灯贪心暴力 DFS生成树两子树差调水 GCD树连通块暴力

    A 水题 /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) using namespace ...

  7. Codeforces 987 K预处理BFS 3n,7n+1随机结论题/不动点逆序对 X&Y=0连边DFS求连通块数目

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...

  8. DFS序+线段树 hihoCoder 1381 Little Y's Tree(树的连通块的直径和)

    题目链接 #1381 : Little Y's Tree 时间限制:24000ms 单点时限:4000ms 内存限制:512MB 描述 小Y有一棵n个节点的树,每条边都有正的边权. 小J有q个询问,每 ...

  9. UVA 572 油田连通块-并查集解决

    题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...

  10. HD1269迷宫城堡(有向图 && 划分连通块)

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. Appium-xpath详解

    一.xpath简介 XPath就是XML 路径,练习XPath的使用可以直接使用火狐浏览器 火狐浏览器下载 free bug和free path两个插件. 手机xpath可以自己写,路径关键字选cla ...

  2. 通过rtmpdump推送海康视频流到red5服务器

    现在主流的网络摄像机都支持标准H264视频格式,例如 海康网络摄像机, 通过海康提供的网络SDK可以获取到视频码流.我测试的这款相机,视频编码采用的是H264,音频编码采用的是G711a. 这里,我仅 ...

  3. poj 3469 Dual Core CPU——最小割

    题目:http://poj.org/problem?id=3469 最小割裸题. 那个限制就是在 i.j 之间连双向边. 根据本题能引出网络流中二元关系的种种. 别忘了写 if ( x==n+1 ) ...

  4. bzoj 1927 星际竞速 —— 最小费用最大流

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1927 首先注意到这是个DAG: 考虑每个点从哪里来,可以是瞬移来的,也可以是从某个点走过来的 ...

  5. 关于系统中:/dev/mem

    1)参考:https://blog.csdn.net/lsn946803746/article/details/52948036   博主:lsn946803746 2)参考:https://blog ...

  6. Nmon工具的使用以及通过nmon_analyse生成分析报表

    在我们监控我们的操作系统的时候如果可以把各个硬件的监控信息生成形象化的分析报表图对于我们来说是件太好的事情了,而通过ibm的nom和nmon_analyser两者的结合完全可以实现我们的要求.首先对n ...

  7. JS开发中的一些小技巧和方法

    生成指定范围内的随机数 当我们需要获取指定范围(min,max)内的整数的时候,下面的代码非常适合:这段代码用的还挺多的. function setRadomNum(min,max){ return ...

  8. js插件库+bootstrap

    1.Chart.js 官网地址:http://chartjs.cn/ 2.优秀的bootstrap模板推荐 官网地址:http://bootswatch.com 3.wow+animate+js插件库 ...

  9. Eclipse/MyEclipse下如何Maven管理多个Mapreduce程序?(企业级水平)

    不多说,直接上干货! 如何在Maven官网下载历史版本 Eclipse下Maven新建项目.自动打依赖jar包(包含普通项目和Web项目) Eclipse下Maven新建Web项目index.jsp报 ...

  10. IPC编程之共享内存

    一,共享内存介绍 共享内存是三个IPC(Inter-Process Communication)机制中的一个,它允许两个不相关的进程访问同一个逻辑内存.   二.共享内存使用的函数 #include ...