2015 ACM Arabella Collegiate Programming Contest(F題),arabellacollegiate

來源:互聯網
上載者:User

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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.