带权并查集: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 ,找一个点当做首都,其 ...
随机推荐
- 《深入理解java虚拟机》笔记(7)JVM调优(分代垃圾收集器)
以下配置主要针对分代垃圾回收算法而言. 一.堆大小设置 年轻代的设置很关键 JVM中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用 ...
- SpringBoot+Vue前后端分离,使用SpringSecurity完美处理权限问题(一)
当前后端分离时,权限问题的处理也和我们传统的处理方式有一点差异. 笔者前几天刚好在负责一个项目的权限管理模块,现在权限管理模块已经做完了,我想通过5-6篇文章,来介绍一下项目中遇到的问题以及我的解决方 ...
- React Router 4.0中文快速入门
import React from 'react' import { BrowserRouter as Router, Route, Link } from 'react-router-dom' co ...
- 报错:java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.xxx.entity.PersonEntity
报错:java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.xxx.entity.PersonEntity 代 ...
- C#小记
1.背景:用fileinput 上传文件 直接上传文件,但有时会发现,这个不上传文件也是可以携带其他参数的, 如果直接用: uploadFile = context.Request.Files[]; ...
- 评价PE基金绩效的常用指标
作为信息系统,辅助管理层决策是重要的功能之一.前文介绍了PE基金管理系统的建设,对PE业务的运转有了一些了解,但没有介绍如何评价PE基金的绩效,而这是管理层作出重大决策的主要依据之一.PE基金本质也是 ...
- Ubuntu 12.04源
deb http://ubuntu.uestc.edu.cn/ubuntu/ precise main restricted universe multiverse deb http://ubuntu ...
- Android 模仿苹果虚拟悬浮按钮(自动靠边、可浮现任何界面上)
由于最近小蔡的手机音量键坏了,调节音量有点麻烦,突发奇想,想自己实现一个快捷键来调节音量.在忘上参考了一些代码,总结出一般本章,分享给大家. 首先 按钮要想实现悬浮在任何界面,那么必须是要写在服务里面 ...
- 【虚拟机-部署】通过 Powershell 来调整 ARM 模式下虚拟机的尺寸
需求描述 在部署完 ARM 模式的虚拟机以后,可以通过 PowerShell 命令来调整虚拟机的尺寸,以下是通过 PowerShell 命令来调整 ARM 模式的虚拟机尺寸. Note 本文只限于 A ...
- Git项目管理常用命令
安装Git Bash后,地址:https://git-scm.com/downloads 根据自己的操作系统选择对应是安装方式 可参见码云给出的文档:http://git.mydoc.io/?t=18 ...