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 ...
随机推荐
- cts测试流程
测试目的: 用于检测你做的Android系统是否满足兼容性要求,通俗点说,Google认为Android系统应该满足的条件,你需要满足. 例如框架层暴露给应用层的某些接口,Google认为你因该有,那 ...
- Convolutional Neural Networks(5):Pooling Layer
池化层(Pooling layer)同样是收到了视觉神经科学的启发.在初级视觉皮层V1(Primary visual cortex)中,包含了许多复杂细胞(Complex cells),这些细胞对于图 ...
- Vulnhub渗透测试练习(一) ----------Breach1.0
教程网址 https://www.freebuf.com/articles/system/171318.html 学习经验总结 1.使用jre的bin目录下的keytool命令来输入秘钥库口令进而获取 ...
- [Linux] 019 软件包管理简介
1. 软件包分类 源码包 脚本安装包 二进制包(RPM 包.系统默认包) 2. 源码包 (1)源码包的优点 开源,如果有足够的能力,可以修改源代码 可以自由选择所需的功能 软件是编译安装,所以更加适合 ...
- 诊断:MRP0: Background Media Recovery process shutdown with error ORA-19909
oracle12c data guard,从库无法应用日志,检查alert日至发现 2019-10-21T14:55:40.087819+08:00 MRP0: Background Media Re ...
- Java语言的特点与工作原理
Java语言的特点 1.简单性 Java语言与我们常听到的C++语言很像,但是没有C++那么繁琐.因为Java就是在C++之上设计出来的,设计者把C++的一些特性去掉了,这些特性在实际开发中,程序员也 ...
- 2.openshift授权策略和scc理解
policy管理概念 查看policy的方式可以通过cli进行查看 Roles grant various levels of access in the system-wide cluster po ...
- XMPP即时通讯协议使用(三)——订阅发布、断开重连与Ping
package com.testV3; import java.util.List; import org.jivesoftware.smack.ConnectionListener; import ...
- Spring Boot 2 Webflux的全局异常处理
https://www.jianshu.com/p/6f631f3e00b9 本文首先将会回顾Spring 5之前的SpringMVC异常处理机制,然后主要讲解Spring Boot 2 Webflu ...
- Kvm --01 虚拟化基础概念
目录 1. 虚拟化基础概念 01. 什么是虚拟化? 02. 为什么要用虚拟化? 03. 虚拟化在企业中的应用场景? 04. 虚拟化软件介绍 05. Kvm介绍 2. 安装部署Kvm 3. Kvm虚拟机 ...