Gym-100676F Palindrome
原题连接:https://odzkskevi.qnssl.com/1110bec98ca57b5ce6aec79b210d2849?v=1491063604
题意:
多组输入,每一次输入一个n(字符串的长度),一个m(下面m条关系),一个字符串s,m条关系(要求x, y位的字符相等)。
字符串中有一个或者多个'?' ,'?'可以随机填26个小写英文字母。问你这样的字符串可以有多少种填写方法。
解题思路:
这一道题,用并查集的方法可以很快的解决。
因为第i个字符是一定与第n-1-i个字符相同的(i从0开始),加上m条要求之后,就可以获得多个集合,每一个集合都是要求相同的字符。
选择父亲的时候有讲究,尽量选择是字符作为祖先,'?' 插入到祖父的下面。
然后就是便利处理一下,把可以确定的 '?' 转换成字母。(只有全部是 '?' 的集合不能转换成字母)
最后就是遍历一下在字符串中 '?' 的集合的个数,答案就出来了。
****************************************************************************************************
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define mset(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = 1e9+;
char s[maxn];
int father[maxn];
int vis[maxn];
int findfa(int x)
{
return father[x]==-? x : father[x] = findfa(father[x]);
}
void uni(int x, int y)
{
int fa1 = findfa(x);
int fa2 = findfa(y);
if(fa1!=fa2){
if(s[fa1]=='?') father[fa1] = fa2;
else father[fa2] = fa1;
}
}
int main()
{
#ifdef LOCAL
freopen("input.txt" , "r", stdin);
#endif // LOCAL
int T;
cin >> T;
while(T--){
int n, m, x, y, flag = , num=;
mset(father, -);
mset(vis, );
scanf("%d%d%s",&n,&m, s);
for(int i=;i<n;i++) uni(i, n--i);
for(int i = ;i<m;i++){
scanf("%d%d", &x, &y);
uni(--x, --y);
} for(int i=;i<n;i++){
int x = findfa(i);
if(s[i] == '?' && s[x] != '?') s[i] = s[x];
if(s[i] !='?' && s[x] !='?' && s[i]!=s[x]) {flag=;break;}//如果字母和字母不对应,直接错误。
}
if(!flag) printf("0\n");
else{
for(int i=;i<n;i++){
if(s[i]=='?' && father[i] == -){
num++;
}
}
LL ans = 1LL;
for(int i = ;i<num;i++) ans = (1LL*ans* )%mod;
printf("%lld\n", ans);
}
}
return ;
}
****************************************************************************************************
Gym-100676F Palindrome的更多相关文章
- Gym - 100676F Palindrome —— 并查集
题目链接:https://vjudge.net/contest/155789#problem/E 题解: 由于是回文串,所以可以先将在对称位置的字符放在同一个集合(如果期间有两个非‘?’,且不相等,则 ...
- Gym 100570E : Palindrome Query
De Prezer loves palindrome strings. A string s1s2...sn is palindrome if and only if it is equal to i ...
- Codeforces Gym 100570 E. Palindrome Query Manacher
E. Palindrome QueryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100570/pro ...
- Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】
H. Special Palindrome time limit per test:1 second memory limit per test:64 megabytes input:standard ...
- Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】
C. Palindrome Again !! time limit per test:1 second memory limit per test:64 megabytes input:standar ...
- Gym 100952 H. Special Palindrome
http://codeforces.com/gym/100952/problem/H H. Special Palindrome time limit per test 1 second memory ...
- Gym 100952 C. Palindrome Again !!
http://codeforces.com/gym/100952/problem/C C. Palindrome Again !! time limit per test 1 second memor ...
- Gym - 100570E:Palindrome Query (hash+BIT+二分维护回文串长度)
题意:给定字符串char[],以及Q个操作,操作有三种: 1:pos,chr:把pos位置的字符改为chr 2:pos:问以pos为中心的回文串长度为多长. 3:pos:问以pos,pos+1为中心的 ...
- codeforces gym 100971 K Palindromization 思路
题目链接:http://codeforces.com/gym/100971/problem/K K. Palindromization time limit per test 2.0 s memory ...
- PALIN - The Next Palindrome 对称的数
A positive integer is called a palindrome if its representation in the decimal system is the same wh ...
随机推荐
- 使EasyUI的window、panel、dialog 不被拖出页面
function easyuiPanelOnMove(left, top) { var l = left; var t = top; if (l < 1) { l = 1; } ...
- Spring学习(四)--面向切面的Spring
一.Spring--面向切面 在软件开发中,散布于应用中多处的功能被称为横切关注点(cross- cutting concern).通常来讲,这些横切关注点从概念上是与应用的业 务逻辑相分离的(但是往 ...
- (vue.js)axios interceptors 拦截器中添加headers 属性
(vue.js)axios interceptors 拦截器中添加headers 属性:http://www.codes51.com/itwd/4282111.html 问题: (vue.js)axi ...
- python读取ini文件时,特殊字符的读取
前言: 使用python在读取配置文件时,由于配置文件中存在特殊字符,读取时出现了以下错误: configparser.InterpolationSyntaxError: '%' must be fo ...
- iview table列中根据不同的状态显示不同的颜色,显示图片
使用reder可以实现 1.显示不同状态 2.显示图片
- JS小案例--全选和全不选列表功能的实现
纯js代码实现列表全选和全不选的功能 <!DOCTYPE html> <html> <head lang="en"> <meta char ...
- centos7解决ssh登录速度慢的问题
先备份/etc/ssh/sshd_config,备份命令为 cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak 1.su ...
- sq - 压缩一个排过序的单词列表 unsq - 解压一个排过序的单词列表
总览 (SYNOPSIS) sq < infile > outfile unsq < infile > outfile 描述 (DESCRIPTION) sq 压缩 一个 排过 ...
- K8S创建的相关yaml文件
一.K8S-yaml的使用及命令 YAML配置文件管理对象 对象管理: # 创建deployment资源 kubectl create -f nginx-deployment.yaml # 查看dep ...
- 2017ICPC沈阳赛现场赛 L-Tree (dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6228 题目大意:给一棵树,需要用k种颜色给树上的节点染色,问你在最优的染色方案下,相同颜色的节点连接的 ...