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 ...
随机推荐
- ubuntu下可用的串口调试工具--cutecom
今天在ubuntu下要用到串口发送16进制数据,百度了很多工具,觉得minicom和cutecom都不错,比较直观是cutecom,所以就介绍下cutecom. 安装: 输入 $ sudo apt-g ...
- linux下shell显示git当前分支
function git-branch-name { git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3 } functio ...
- Spring学习(六)--渲染Web视图
一.将模型数据渲染为Html 在上一篇文章中,我们所编写的控制器方法都没有直接产生浏览器中渲染所需的HTML.这些方法只是将数据填充到模型中,然后将模型传递给一个用来渲染的视图.这些方法会返回一个St ...
- 06 CAS的原理和AQS
CAS的原理 CAS(compareAndSwap),比较交换,是一种无锁的原子算法. Cas(value,expect,newValue),如果vaule和ecpect一样,就更新为newValue ...
- 攻防世界--getit
测试文件:https://adworld.xctf.org.cn/media/task/attachments/8ef2f7ef55c240418f84b3c514a7a28a 准备 得知 64位文件 ...
- vue-ivew input 框 回车搜索功能
1. 添加事件 <FormItem prop="> <Input type="text" v-model="formInline.produc ...
- hdu4336 Card Collector MinMax 容斥
题目传送门 https://vjudge.net/problem/HDU-4336 http://acm.hdu.edu.cn/showproblem.php?pid=4336 题解 minmax 容 ...
- oracle中分页函数写法
1.常见的分页查询语句: 查询21到40条之间的数据:SELECT *FROM (select UI.*,ROWNUM RN FROM (select * from user_info) AWHERE ...
- 推荐Html Table和Markown互转的网站Table Convert Online
网站名称:https://tableconvert.com/ 进入网站可以看到可以Table 转为Markdown.JSON.XML.SQL 多种格式 Table(4×5)定义Table的行数和列数: ...
- 【leetcode】336. Palindrome Pairs
题目如下: 解题思路:对于任意一个word,要找出在wordlist中是否存在与之能组成回文的其他words,有两种思路.一是遍历wordlist:二是对word本身进行分析,找出能组成回文的word ...