Gym 100676F Palindrome(并差集)
来源:互联网 发布:阿里云dns解析怎么用 编辑:程序博客网 时间:2024/06/14 19:01
链接:http://codeforces.com/gym/100676/attachments/download/3333/acm-arabella-collegiate-programming-contest-en.pdf
题目:F - F. Palindrome
题意:给你一个字符串,?处是没有填的地方,然后他是一个回文串并且告诉你一些其他位置的字母是需要相等的。问你:有多少种满足条件的填法。
分析:这题比较简洁的做法就是并差集。首先把所有能够确定的?确定了,再考虑不能确定的?的个数,其实就是用并差集集合并集合。优先吧字母作为根节点,处理后如果一个集合的根节点是?,那么这个集合里所有的元素都不确定,而且它们取同一个字母,不确定的?+1.
注意:没有?时是1,只是隐含在黑暗处的Bug.
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;const int mod=1000000007;const int maxn=50010;char s[maxn];int f[maxn];int n,m,x,y,sum,t,len,ff;long long ans;int find(int x){ return (f[x]==x?x:f[x]=find(f[x]));}void cal(int x,int y){ if(s[x] == s[y]) f[x]=y; else if(s[x] == '?') f[x]=y; else if(s[y] == '?') f[y]=x; else ff=1;}int init(){ int ans=0; ff=0; len=strlen(s); for(int i=0;i<len;i++) f[i]=i; for(int i=0,j=len-1;i<j;i++,j--) cal(i,j); for(int i=0;i<m;i++){ scanf("%d %d",&x,&y); cal(find(--x),find(--y)); } if(ff==1) return -1; for(int i=0;i<len;i++) if(s[i]=='?' && f[i]==i) ans++; return ans;}int main(){ scanf("%d",&t); while(t--) { scanf("%d%d%s",&n,&m,&s); sum = init(); if(sum<1) {printf("%d\n",sum+1);continue;} ans=1; for(int i=0;i<sum;i++) ans = (ans*26)%mod; printf("%lld\n",(ans+mod)%mod); } return 0;}
0 0
- Gym 100676F Palindrome(并差集)
- Gym 100712F Travelling Salesman(二分+并查集)
- GYM 101243 F.Vitamins【思维+并查集】
- [Gym-101350F] F
- Gym 100685F Flood(拓扑排序)
- Gym - 100512F Funny Game(杂题)
- Gym - 101142F Folding(折半)
- codeforces gym 101142 F(思维)
- GYM 100247 F. Battle Fury(二分)
- GYM 100090 F.Asperger Syndrome(set)
- GYM 100488 F.Two Envelopes(水~)
- GYM 101102 F.Exchange(水~)
- GYM 101173 F.Free Figurines(贪心)
- GYM 100971 F.Two Points(三分)
- GYM 101086 F.Hey JUDgE(枚举)
- GYM 101061 F.Fairness(dp)
- Gym 101201F Illumination(2-sat)
- F. Palindrome
- storm trident 自定义spout
- python中操作MySQLdb
- MAC OS 下安装cocoapods安装的错误问题
- TF-IDF技术百科
- JSP页面跳转的五种方法
- Gym 100676F Palindrome(并差集)
- 关系数据库设计三范式
- wordpress有效解决wordpress加载谷歌字体慢的最新方法
- 2016年阿里巴巴校招笔试题(客户端开发岗位)
- 黑马学习笔记_多线程
- 【机房收费系统】组合查询
- Struts(20)拦截器
- 互联网行业的那些缩写PM,RD,FE,UE,QA,OP,BRD,MRD,PRD,FSD
- C Looooops(扩展欧几里得)