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


解题心得:

  1. 这个题的题意就是给你一个字符串,里面包含小写字母和?,字符串必须是回文串并且给你m对数,该数位置对应的字母应该相等。?可以替换成任意的26个小写字母,问一共有多少种形成规定回文串的可能。
  2. 在做这个题的时候是比赛,比赛很简单,这个题是最后一个,没想到是一个简单的带权并查集,当时在用搜索硬怼,写了一大堆代码,很恼火,还是不够冷静啊。
  3. 就这个题来说如果细分很麻烦,又是回文又是位置对应还有?替换。其实思维到位了就完全不用那么麻烦,直接将该合并的合并,回文位置合并,对应位置合并,合并之后在从根节点开始找,看同一个根的集合里面的元素,如果不对应,不回文的直接输出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题)的更多相关文章

  1. 2015 ACM Arabella Collegiate Programming Contest

    题目链接:https://vjudge.net/contest/154238#overview. ABCDE都是水题. F题,一开始分类讨论,结果似乎写挫了,WA了一发.果断换并查集上,A了. G题, ...

  2. gym100676 [小熊骑士限定]2015 ACM Arabella Collegiate Programming Contest

    Kuma Rider久违的第二场训练,这场很水,又在vj的榜单上看到第一场的大哥了,2小时ak,大哥牛啤! A.水 #include<cstdio> #include<iostrea ...

  3. Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest(二月十日训练赛)

    A(By talker): 题意分析:以a(int) op b(int)形式给出两个整数和操作符, 求两个整数是否存在操作符所给定的关系 ,有则输出true,无则输出false: 思路:由于无时间复杂 ...

  4. 边双连通缩点+树dp 2015 ACM Arabella Collegiate Programming Contest的Gym - 100676H

    http://codeforces.com/gym/100676/attachments 题目大意: 有n个城市,有m条路,每条路都有边长,如果某几个城市的路能组成一个环,那么在环中的这些城市就有传送 ...

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

  6. ACM Arabella Collegiate Programming Contest 2015 F. Palindrome 并查集

    题目链接:http://codeforces.com/gym/100676/attachments 题意: 给一个字符串,有一些约束条件,两个位置要相同,有一些是问号,求最后有多少种方案回文? 分析: ...

  7. 2015 ACM Amman Collegiate Programming Contest 题解

    [题目链接] A - Who Is The Winner 模拟. #include <bits/stdc++.h> using namespace std; int T; int n; s ...

  8. 2015 ACM Syrian Collegiate Programming Contest

    A. My Friend of Misery 计算出答案的上下界即可. 时间复杂度$O(n)$. #include<bits/stdc++.h> using namespace std; ...

  9. ACM Arabella Collegiate Programming Contest 2015 H. Capital City 边连通分量

    题目链接:http://codeforces.com/gym/100676/attachments 题意: 有 n 个点,m 条边,图中,边强连通分量之间可以直达,即距离为 0 ,找一个点当做首都,其 ...

随机推荐

  1. F. Clique in the Divisibility Graph DP

    http://codeforces.com/contest/566/problem/F F. Clique in the Divisibility Graph time limit per test ...

  2. Swagger 2.0 集成配置

    传统的API文档编写存在以下几个痛点: 对API文档进行更新的时候,需要通知前端开发人员,导致文档更新交流不及时: API接口返回信息不明确 大公司中肯定会有专门文档服务器对接口文档进行更新. 缺乏在 ...

  3. 在linux上怎么查看tomcat日志

    进入到tomcat的logs文件夹 tail -f catalina.out

  4. 给Eclipse设置android的SDK位置时,出现这个:This Android SDK requires Andr...ate ADT to the latest

    这样的问题很好解决,一个升级ADT到指定版本或以上,另一个简单的办法是调低SDK版本 找到android-sdk-windows\tools\lib下的plugin.prop文件修改其中的版本号,改为 ...

  5. 搭建本地SVN資料

    基于網上眾多教程,搭建SVN成功:VisualSVN Server + TortoiseSVN Client. 過程比較簡單,就不重複書寫了. 部份參考資料,感謝作者: 什麽是SVN及如何應用 htt ...

  6. 兼容IE9以下的获取兄弟节点

    function fileCheck(ele){ function getNextElement(node){ //兼容IE9以下的 获取兄弟节点 var NextElementNode = node ...

  7. 【extjs6学习笔记】1.5 初始:关于布局

    absolute 绝对布局,这个布局使用 x 和 y 属性来指定组件的绝对定位 accordion 手风琴布局[可折叠布局]这个布局展示了在一个时间里只有一个内置的可支持折叠和展开的子级 panel ...

  8. Python+Selenium之断言对应的元素是否获取以及基础知识回顾

    # coding=utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.maximize_window () ...

  9. asp页面无法访问,可尝试开始SQL Server等服务

    存在问题 asp页面的英文提示,翻译后为: "一个错误发生在服务器在处理URL.请联系系统管理员(管理人).如果您是系统管理员,请点击这里了解更多关于这个错误."   解决方案 请 ...

  10. Red Hat Enterprise Linux(RHEL)中yum的repo文件详解

    Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器.基于RPM包管理,能够从指定的服务器自动下载 ...