ACM Arabella Collegiate Programming Contest 2015 F. Palindrome 并查集
2024-10-21 05:57:43
题目链接:http://codeforces.com/gym/100676/attachments
题意:
给一个字符串,有一些约束条件,两个位置要相同,有一些是问号,求最后有多少种方案回文?
分析:
每一个节点是一个集合,要是不同,有一个是问号,那么这个问号就是确定的(约束条件中,和回文的对称位置),单独的集合,他又是问号,就可以放26个字母了;
#include <iostream>
#include <cstdio>
#include <cstring> using namespace std; const int MAXN = ; char s[MAXN];
int n,m;
int f[MAXN],flag; int Find(int x)
{
if(x == f[x])
return x;
else return f[x] = Find(f[x]);
} void judge(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 flag = true;
} int main()
{
//freopen("in.txt","r",stdin);
int ncase;
scanf("%d",&ncase);
while(ncase--) {
flag = false;
scanf("%d%d%s",&n,&m,s);
for(int i = ;i < n; i++)
f[i] = i;
for(int i = ,j = n-;i < j; i++,j--) {
judge(i,j);
}
for(int i = ;i < m; i++) {
int a,b;
scanf("%d%d",&a,&b);
a--,b--;
judge(Find(a),Find(b));
}
if(flag) {
printf("0\n");
continue;
}
else {
long long ans = ;
for(int i = ;i < n; i++) {
if(f[i] == i && s[i] == '?') {
ans *= ;
ans %= ;
}
}
printf("%lld\n",ans);
}
}
return ;
}
最新文章
- js删除数组指定元素
- Thinkphp批量添加数据
- CentOS 7 做服务器 CentOS 5 做客服机 搭建Apache+php+mysql网页
- Java中long类型直接赋值大数字 注意事项
- poj 2704 Pascal&#39;s Travels_记忆化搜索
- C#中List和数组之间的转换
- 3、Spring的AOP详解和案例
- LINQ学习系列-----1.3 扩展方法
- NFS介绍和安装
- python_特殊函数
- Pytorch入门实例:mnist分类训练
- Spring系列(六) Spring Web MVC 应用构建分析
- 【NOI2002】
- Win10系列:JavaScript小球运动示例
- openstack 资料
- VC工程从Win32环境往Win64环境迁移的经验总结
- Python3与Python2的差异
- 【小程序】text-indent设置
- Log Structured Merge Trees (LSM)
- target属性用于返回最初触发事件的DOM元素。