CodeForces 123A 并查集
来源:互联网 发布:海量数据 编辑:程序博客网 时间:2024/06/01 08:39
写了好长时间的代码,思路一看就有了,然后写了下,发现自己这方面的题目写的真是不好。。。擦。
题意:给一个字符串,然后对于这个字符串来说,他的素数位置 * i (1=<i <= len)要等于他本身,从新构造这个字符串,判断是否有一种情况满足这个条件。。
/* 这个代码自己写的真挫,有思路但是写不出来。。。。。*/#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<queue>using namespace std;int father[10001];int find(int x){ if(x == father[x])return x; else return father[x] = find(father[x]);}int main(){ char a[1009],b[1009]; cin >> (a+1); int len = strlen(a+1); int vis[1008]; for(int i=1;i<=len;i++)father[i] = i; for(int i=2;i<=len;i++){ for(int j=i;j<=len;j+=i){ if(vis[j]){ int x = find(j); father[i] = x; } else { father[j] =i;vis[j] = 1; } } } int numa[1008]; memset(numa,0,sizeof(numa)); for(int i=1;i<=len;i++) numa[a[i]-'a']++; int num[1008]; memset(num,0,sizeof(num)); int cn = 0; int k[1008]; for(int i=1;i<=len;i++) { for(int j=1;j<=len;j++) { if(father[j] == i)num[i]++; } if(num[i])k[cn++] = i; } int flag = 1; for(int i=0;i<cn;i++){ int t=k[i],Max = 10000000,pos =-1; for(int j=0;j<=25;j++){ if(!numa[j])continue; if(numa[j] - num[k[i]] < Max &&numa[j] >=num[k[i]]){ Max = numa[j] - num[k[i]]; t = k[i]; pos = j; } } if(pos == -1){flag = 0;break;} for(int h=1;h<=len;h++){ if(father[h] == t){ b[h] = pos+'a'; numa[pos] --; } } num[t] = 0; } b[len+1] = '\0'; int cnt = 0; if(flag == 0)puts("NO"); else{ puts("YES"); for(int i=1;i<=len;i++) { if(b[i] <'a'){ for(int j=0;j<=25;j++) if(numa[j]>0) { printf("%c",j+'a'); numa[j]--; } } else printf("%c",b[i]); } puts(""); }}
0 0
- CodeForces 123A 并查集
- CodeForces - 744A (并查集)
- codeforces 827A 并查集
- CodeForces 277A Learning Languages 并查集
- CodeForces 277A Learning Languages (并查集)
- CodeForces 217A Ice Skating(并查集思路)
- Codeforces 698C. Fix a Tree (并查集)
- Codeforces 698B Fix a Tree 并查集
- CodeForces 115A Party(并查集)
- 文章标题 codeforces 115A:Party ( 并查集)
- Codeforces 217A. Ice Skating 【并查集】
- codeforces-445【A思维】【B并查集】
- CodeForces 698B - Fix a Tree(并查集)
- codeforces #363 fix a tree 并查集
- Codeforces 699D. Fix a Tree【并查集+思维】
- Codeforces 771A Bear and Friendship Condition[并查集]
- CodeForces 217 A.Ice Skating(并查集)
- CodeForces 277A Learning Languages (并查集)
- NSArchiver (NSCoder : NSObject) <NSObject>
- 最新校招笔试面试六十题
- 使文本框对齐的方法,
- 面试题33 把数组排成最小的数
- NSUnarchiver (NSCoder : NSObject) <NSObject>
- CodeForces 123A 并查集
- 2014年阿里巴巴9月14号笔试题
- 面试题34 丑数
- python库的那些事
- 系统不能从U盘启动
- 如何增加私有mibs
- NSData (NSObject) <NSCopying, NSObject, NSSecureCoding, NSMutableCopying>
- orace 创建一个job
- JAVA操作JS函数