带权并查集:CF-2015 ACM Arabella Collegiate Programming Contest(F题)
F. Palindrome
Problem Description
A string is palindrome if it can be read the same way in either direction, for example “maram” is palindrome, while “ammar” is not.
You are given a string of n characters, where each character is either a lowercase English letter or a question mark (?). You are also given a set of m constraints. Your task is to count the number of ways in which we can replace all question marks with lowercase English letters such that the resulting string is a palindrome that does not violate the given constraints.
The constraints are in the form of pairs of indices of letters that should be the same.
Input
The first line of input contains one integer T, the number of test cases (1 ≤ T ≤ 256).
Each test case begins with two integers: n representing the string size (1 ≤ n ≤ 50000) and m representing the number of constraints (0 ≤ m ≤ 10000).
The next line contains a string of length n. The next m lines, each contains two integers x and y (1 <= x < y <= n), where letters at index x and index y must be the same.
Test cases are separated by a blank line.
Output
For each test case, print the number of ways modulo 1,000,000,007 (10^9 + 7).
Sample Input
4
5 1
ma??m
1 5
5 4
ma??m
1 2
1 5
1 3
3 4
7 0
acm?cpc
4 1
????
1 4
Sample Output
26
0
0
676
解题心得:
- 这个题的题意就是给你一个字符串,里面包含小写字母和?,字符串必须是回文串并且给你m对数,该数位置对应的字母应该相等。?可以替换成任意的26个小写字母,问一共有多少种形成规定回文串的可能。
- 在做这个题的时候是比赛,比赛很简单,这个题是最后一个,没想到是一个简单的带权并查集,当时在用搜索硬怼,写了一大堆代码,很恼火,还是不够冷静啊。
- 就这个题来说如果细分很麻烦,又是回文又是位置对应还有?替换。其实思维到位了就完全不用那么麻烦,直接将该合并的合并,回文位置合并,对应位置合并,合并之后在从根节点开始找,看同一个根的集合里面的元素,如果不对应,不回文的直接输出0就行了,只有该集合全部是?的才可以用answer乘以26(因为一个集合里面的?必须相等)。每一个集合里面的字母必须全部相等,或者全是?。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 5e4+100;
const int MOD = 1e9+7;
char s[maxn];
int father[maxn];
vector <int> ve[maxn];
int find(int x)
{
if(father[x] == x)
return x;
else
return father[x] = find(father[x]);
}
int main()
{
int len,m;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&len,&m);
for(int i=0; i<=len; i++)
{
father[i] = i;
ve[i].clear();
}
scanf("%s",s);
//对应位置的不用管直接合并
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
a--,b--;
int fa = find(a);
int fb = find(b);
if(fa != fb)
father[fa] = fb;
}
int Len = len / 2;
//回文位置的不用管也直接合并
for(int i=0; i<len; i++)
{
int fa = find(i);
int fb = find(len-1-i);
if(fa!=fb)
father[fa] = fb;
}
bool flag = false;//用来记录是否是符合规定的回文串
//同一根节点的集合全部记录下来
for(int i=0; i<len; i++)
{
int f = find(i);
ve[f].push_back(i);
}
long long ans = 1;
for(int i=0; i<len; i++)
{
char c = 'A',cnt = 0;
Len = ve[i].size();
if(!Len)
continue;
for(int j=0; j<Len; j++)
{
if(s[ve[i][j]] == '?')//该集合里面的?的数目记录一下
cnt++;
else if(s[ve[i][j]] != c && c == 'A')//记录该集合里面的字幕
c = s[ve[i][j]];
else if(s[ve[i][j]] != c && c != 'A')//一个集合里面出现了两种不同的字母,不符合要求
flag = true;
}
if(Len == cnt)//一个集合里面全是?(所有?必须替代成同一个字母)
{
ans = (ans * 26)%MOD;
}
}
if(flag)
{
printf("0\n");
continue;
}
printf("%d\n",ans);
}
}
带权并查集:CF-2015 ACM Arabella Collegiate Programming Contest(F题)的更多相关文章
- 2015 ACM Arabella Collegiate Programming Contest
题目链接:https://vjudge.net/contest/154238#overview. ABCDE都是水题. F题,一开始分类讨论,结果似乎写挫了,WA了一发.果断换并查集上,A了. G题, ...
- gym100676 [小熊骑士限定]2015 ACM Arabella Collegiate Programming Contest
Kuma Rider久违的第二场训练,这场很水,又在vj的榜单上看到第一场的大哥了,2小时ak,大哥牛啤! A.水 #include<cstdio> #include<iostrea ...
- Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest(二月十日训练赛)
A(By talker): 题意分析:以a(int) op b(int)形式给出两个整数和操作符, 求两个整数是否存在操作符所给定的关系 ,有则输出true,无则输出false: 思路:由于无时间复杂 ...
- 边双连通缩点+树dp 2015 ACM Arabella Collegiate Programming Contest的Gym - 100676H
http://codeforces.com/gym/100676/attachments 题目大意: 有n个城市,有m条路,每条路都有边长,如果某几个城市的路能组成一个环,那么在环中的这些城市就有传送 ...
- 18春季训练01-3/11 2015 ACM Amman Collegiate Programming Contest
Solved A Gym 100712A Who Is The Winner Solved B Gym 100712B Rock-Paper-Scissors Solved C Gym 100712C ...
- ACM Arabella Collegiate Programming Contest 2015 F. Palindrome 并查集
题目链接:http://codeforces.com/gym/100676/attachments 题意: 给一个字符串,有一些约束条件,两个位置要相同,有一些是问号,求最后有多少种方案回文? 分析: ...
- 2015 ACM Amman Collegiate Programming Contest 题解
[题目链接] A - Who Is The Winner 模拟. #include <bits/stdc++.h> using namespace std; int T; int n; s ...
- 2015 ACM Syrian Collegiate Programming Contest
A. My Friend of Misery 计算出答案的上下界即可. 时间复杂度$O(n)$. #include<bits/stdc++.h> using namespace std; ...
- ACM Arabella Collegiate Programming Contest 2015 H. Capital City 边连通分量
题目链接:http://codeforces.com/gym/100676/attachments 题意: 有 n 个点,m 条边,图中,边强连通分量之间可以直达,即距离为 0 ,找一个点当做首都,其 ...
随机推荐
- 汉柏杯&&政治生日6月5日&&端午节
(一)汉柏杯 前不久汉柏杯2019年计算机设计大赛由我校承办,参加了软件应用开发组竞赛.开发了一个基于微信公众号的求职招聘系统,虽然很low但是貌似还是进了国赛,大概八月十号去安徽芜湖参加国赛决赛.据 ...
- memcache和iptables开启11211端口
linux下安装完memcached后,netstat -ant | grep LISTEN 看到memcache用的11211端口已在监听状态,但建立php文件连接测试发现没有输出结果,iptabl ...
- 关于Memcache的连接
addServer 在说Memcache的长连接(pconnect)和短连接(connect)之前要先说说Memcache的addServer,Memcache的addServer是增加一个服务器到连 ...
- spring之控制反转
IOC (Inversion of Control) 控制反转 我的理解:将创建对象的控制权从代码本身转交给了外部容器(spring容器). 1.将组件对象(业务对象)的控制权从代码本身转移到外部容器 ...
- JAVA的API部分介绍
个人理解: Object作为最大的父类,里面存在不少方法,可以在API中具体的查找.比如可以帮助查看是否相同的equals方法,不过要想看具体属性是否相同需要得重写,打印.调用对象相当于调用其tost ...
- Redis set(集合)
Redis 的 Set 是 String 类型的无序集合,元素不允许重复. Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1). 集合中最大的元素数为 232 - 1 ( ...
- ./theHarvester.py -d baidu.com -l 100 -b google
./theHarvester.py -d baidu.com -l 100 -b google
- SharpSvn操作 -- 获取Commit节点列表
/// <summary> /// 获取工作目录的所有节点,包括子目录 /// </summary> /// <param name="workingCopyD ...
- SharePoint 2013 缺少站点保存为模板选项
如果您尝试在SharePoint Server 2013中保存站点,我们没有看到“将站点另存为模板”选项,则可能是因为该站点已启用站点发布功能.如 之前文章提到 “SharePoint 2010 缺少 ...
- js创建弹框(提示框,待确认框)
;;} html,body{text-size-adjust:none;-webkit-text-size-adjust:none;-webkit-user-select:none;} a{color ...