2015 ACM Arabella Collegiate Programming Contest(F題),arabellacollegiate
F. Palindrome
[ Color: Pink ]
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
ACM Arabella Collegiate Programming Contest 2015
題意:給你一個字串,串中只能出現小寫字母和‘?’。'?'可以代替任何小寫字母。現在給你m對(x,y),必須使得第x個字元和第y個字元相同,並且該字串必須為迴文串。現在問組成迴文串的方案數。
分析:並查集。利用並查集把相關的字元關聯起來,三種情況:如果關聯的字元都為'?',ans=ans*26%mod;如果關聯的字元出現了不相同的字元,那麼直接輸出0即可;如果關聯字元出現的字元都相同(可存在'?'),那麼’?‘只能是變成該種字元了,即ans不變,或ans=ans*1。
題目連結:http://codeforces.com/gym/100676
代碼清單:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const ll mod = 1e9 + 7;const int maxn = 50000 +5;int T,n,m,x,y;int pos,len;bool judge;char str[maxn];int father[maxn];ll ans,vis[maxn];int Find(int x){ if(x!=father[x]) return Find(father[x]); return father[x];}void add(int x,int y){ x=Find(x); y=Find(y); if(x!=y) father[y]=x;}void init(){ memset(vis,0,sizeof(vis)); for(int i=0;i<=len;i++) father[i]=i; for(int i=0;i<len/2;i++) add(i,len-i-1); judge=true; ans=1;}int main(){ scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); scanf("%s",str); len=strlen(str); init(); for(int i=0;i<m;i++){ scanf("%d%d",&x,&y); add(x-1,y-1); } for(int i=0;i<len;i++){ pos=Find(i); if(str[pos]!='?'){ if(str[i]=='?') vis[pos]=1; else if(str[i]!=str[pos]){ judge=false; break; } } else{ if(str[i]=='?') vis[pos]=26; else{ str[pos]=str[i]; vis[pos]=1; } } } if(!judge) printf("0\n"); else{ for(int i=0;i<len;i++){ pos=Find(i); if(vis[pos]==26){ ans=ans*26%mod; vis[pos]=1; } } printf("%I64d\n",ans); } }return 0;}