原题连接: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的更多相关文章

  1. Gym - 100676F Palindrome —— 并查集

    题目链接:https://vjudge.net/contest/155789#problem/E 题解: 由于是回文串,所以可以先将在对称位置的字符放在同一个集合(如果期间有两个非‘?’,且不相等,则 ...

  2. Gym 100570E : Palindrome Query

    De Prezer loves palindrome strings. A string s1s2...sn is palindrome if and only if it is equal to i ...

  3. Codeforces Gym 100570 E. Palindrome Query Manacher

    E. Palindrome QueryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100570/pro ...

  4. 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 ...

  5. 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 ...

  6. Gym 100952 H. Special Palindrome

    http://codeforces.com/gym/100952/problem/H H. Special Palindrome time limit per test 1 second memory ...

  7. Gym 100952 C. Palindrome Again !!

    http://codeforces.com/gym/100952/problem/C C. Palindrome Again !! time limit per test 1 second memor ...

  8. Gym - 100570E:Palindrome Query (hash+BIT+二分维护回文串长度)

    题意:给定字符串char[],以及Q个操作,操作有三种: 1:pos,chr:把pos位置的字符改为chr 2:pos:问以pos为中心的回文串长度为多长. 3:pos:问以pos,pos+1为中心的 ...

  9. codeforces gym 100971 K Palindromization 思路

    题目链接:http://codeforces.com/gym/100971/problem/K K. Palindromization time limit per test 2.0 s memory ...

  10. PALIN - The Next Palindrome 对称的数

    A positive integer is called a palindrome if its representation in the decimal system is the same wh ...

随机推荐

  1. mooc-IDEA 调试代码--012

    mooc-IDEA 调试代码 添加断点快捷键:ctrl+F8 单步运行:F9  <=>resum(从一个断点跳转到下一个断点) 一行一行运行:F8 查看所有断点: 禁止所有断点: 条件断点 ...

  2. [Linux] 022 RPM 包查询

    1. 查询是否安装 (1) 查询包是否安装 $ rpm -q 包名 选项 释义 -q (query) 查询 (2) 查询所有已安装的 RPM 包 $ rpm -qa 选项 释义 -a (all) 所有 ...

  3. spring security简单教程以及实现完全前后端分离

    spring security是spring家族的一个安全框架,入门简单.对比shiro,它自带登录页面,自动完成登录操作.权限过滤时支持http方法过滤. 在新手入门使用时,只需要简单的配置,即可实 ...

  4. python爬取企业登记业务

    import requests from lxml import etree import csv for i in range(10, 990, 10): url = "http://12 ...

  5. GnuTLS 3.3.3 remote memory corruption(CVE-2014-3466)

    Buffer overflow in the read_server_hello function in lib/gnutls_handshake.c in GnuTLS before 3.1.25, ...

  6. 图例演示在Linux上快速安装软RAID的详细步骤

    物理环境:虚拟机centos6.4 配置:8G内存.2*2核cpu.3块虚拟硬盘(sda,sdb,sdc,sdb和sdc是完全一样的)        在实际生产环境中,系统硬盘与数据库和应用是分开的, ...

  7. 使用网易云web 版外部链接

    右击打开控制台,找到音乐列表 ——> 点开详情 -------> 生成外部链接器 ,如: outchain/0/3073492173    ,这是每一个音乐的编号. 然后拼接上它的官网域名 ...

  8. 【刷题】java 常见的几种运行时异常RuntimeException

    常见的几种罗列如下: -NullPointerException - 空指针引用异常 ClassCastException - 类型强制转换异常. IllegalArgumentException - ...

  9. sessionStorage 使用方法

    作为html5中Web Storage的一种存储方式,localStorage和sessionStorage一样都是用来存储客户端临时信息的对象. W3c上给的介绍是这两者区别在于前者用于持久化的本地 ...

  10. C中对整数的大端对齐与小端对齐的理解

    示例 /* 大端与小端对齐 说明: 1.对于arm, intel这种x86架构的复杂指令cpu,整数在内存中是 倒着存放的,低地址放低位,高地址放高位,称之为小端对齐 2.对于linux服务器的cpu ...