Problem(M34):Abc Strings

Judge Info

  • Memory Limit: 65537KB
  • Case Time Limit: 2000MS
  • Time Limit: 2000MS
  • Judger: Normal

Description

Let’s define a kind of string of length N which only consists of characters ‘.’, ‘a’, ‘b’, ‘c’. Character ‘.’ must be replaced by ‘a’, ‘b’ or ‘c’. You are asked to count the number of ways to modify such kind of string such that for any ‘b’ in the string , the adjacent character on its right is not a ‘a’ and for any ‘c’ in the string, the adjacent character on its right is not a ‘b’. For example, “aabc” is valid, and “bac”, “acb” are invalid.

Input

The first line is a number T(1<=T<=20) which indicates the number of test cases. For each test case there is a number N(1<=N<=100000) which indicates the length of the string and the following line is the input string.

Output

If the string contains ‘.’ and is impossible to construct such kind of string or the string itself is invalid, output “NO”,else print the number of all valid strings mod 1000,000,007.

Sample Input

2
3
..b
4
.ba.

Sample Output

4
NO


#include <iostream>


#include <cstdio>
#include <cstring>
#include <string.h>
#include <algorithm>


#define mod 1000000007;




#include <map>
using namespace std; 
char f[100005];


int dp[100005][3];    //与前一位的状态有关
int vis[100005][3];  //可能的状态


int main()
{

int i,j,t,n;
scanf("%d",&t);
for (i=1;i<=t;i++)
{
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));


scanf("%d",&n);
scanf("%s",f);

if (f[0]=='a')
{
dp[0][0]=1;
vis[0][0]=1;
}
if (f[0]=='b')
{
dp[0][1]=1;
vis[0][1]=1;
}
if (f[0]=='c')
{
dp[0][2]=1;
vis[0][2]=1;
}
if (f[0]=='.')
{
dp[0][0]=1;
vis[0][0]=1;
dp[0][1]=1;
vis[0][1]=1;
dp[0][2]=1;
vis[0][2]=1;
}

for (j=1;j<n;j++)
{
if (f[j]=='a'||f[j]=='.')
{
if (vis[j-1][0])
{
dp[j][0]=(dp[j][0]+dp[j-1][0])%mod;
vis[j][0]=1;
}
if (vis[j-1][2])
{
dp[j][0]=(dp[j][0]+dp[j-1][2])%mod;
vis[j][0]=1;
}

}
if (f[j]=='b'||f[j]=='.')
{
if (vis[j-1][0])
{
dp[j][1]=(dp[j][1]+dp[j-1][0])%mod;
vis[j][1]=1;
}
if (vis[j-1][1])
{
dp[j][1]=(dp[j][1]+dp[j-1][1])%mod;
vis[j][1]=1;
}

}
if (f[j]=='c'||f[j]=='.')
{
if (vis[j-1][0])
{
dp[j][2]=(dp[j][2]+dp[j-1][0])%mod;
vis[j][2]=1;
}
if (vis[j-1][1])
{
dp[j][2]=(dp[j][2]+dp[j-1][1])%mod;
vis[j][2]=1;
}

if (vis[j-1][2])
{
dp[j][2]=(dp[j][2]+dp[j-1][2])%mod;
vis[j][2]=1;
}

}
}
int k;
int ok=0;
        for(k=0;k<3;k++)
if(vis[n-1][k])
ok=1;
        if(ok==0){
            printf("NO\n");
        }
else
{
int ans=0;
for(k=0;k<3;k++) 
{
ans+=dp[n-1][k];
ans%=mod;
}
printf("%d\n",ans);
}

}







return 0;
}








节省空间算法
利用滚动数组和 last
/*
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define mod 1000000007
char f[100005];
int dp[2][4]; 
int main()
{
int i,j,t;
scanf("%d",&t);
for (i=1;i<=t;i++)
{
memset(dp,0,sizeof(dp)); 
 
int n;
scanf("%d",&n);
scanf("%s",f);
 
int flag=0;
char last=f[0];
for (j=1;j<n;j++)
{
if (last=='b'  && f[j]=='a' || last=='c'&&f[j]=='b'  )
{
printf("NO\n");
flag=1;
break;
}
else
last=f[j]; 
}
if (flag) continue;
 
if (f[0]=='.')
{
dp[0][0]=1;
dp[0][1]=1;
dp[0][2]=1;
}
if (f[0]=='a')
dp[0][0]=1;
if (f[0]=='b')
dp[0][1]=1;
if (f[0]=='c')
dp[0][2]=1;
 
 
 
last=f[0]; 
 
for (j=1;j<n;j++)
{
 
if (f[j]=='a')
{
if ( last=='a')
dp[j%2][0]=(dp[((j-1)%2)][0])%mod;
 
if ( last=='c')
dp[j%2][0]=(dp[((j-1)%2)][2])%mod;
 
if (last=='.')
dp[j%2][0]=(dp[((j-1)%2)][0]+dp[((j-1)%2)][2])%mod;
 
 
}
 
if (f[j]=='b')
{
if (last=='a')
dp[j%2][1]=(dp[((j-1)%2)][0])%mod;
 
if (last=='b')
dp[j%2][1]=(dp[((j-1)%2)][1])%mod;
 
if (last=='.')
dp[j%2][1]=(dp[((j-1)%2)][1]+dp[((j-1)%2)][0])%mod;
 
}
 
if (f[j]=='c')
{
if (last=='a')
dp[j%2][2]=(dp[((j-1)%2)][0])%mod;
 
if (last=='b')
dp[j%2][2]=(dp[(j-1)%2][1])%mod;
 
if (last=='c')
dp[j%2][2]=(dp[(j-1)%2][2])%mod;
 
if (last=='.')
{
dp[j%2][2]=(dp[j%2][2]+dp[(j-1)%2][0])%mod;
 
dp[j%2][2]=(dp[j%2][2]+dp[(j-1)%2][1])%mod;
 
dp[j%2][2]=(dp[j%2][2]+dp[(j-1)%2][2])%mod;
}
 
 
}
 
if (f[j]=='.')
{
if (last=='a')
{
dp[j%2][0]=(dp[(j-1)%2][0])%mod;
dp[j%2][1]=(dp[(j-1)%2][0])%mod;
dp[j%2][2]=(dp[(j-1)%2][0])%mod;
 
 
}
if (last=='b')
{
dp[j%2][1]=(dp[(j-1)%2][1])%mod;
dp[j%2][2]=(dp[(j-1)%2][1])%mod;
 
}
if (last=='.')
{
dp[j%2][0]=(dp[j%2][0]+dp[(j-1)%2][0])%mod;
dp[j%2][1]=(dp[j%2][1]+dp[(j-1)%2][0])%mod;
dp[j%2][2]=(dp[j%2][2]+dp[(j-1)%2][0])%mod;
dp[j%2][1]=(dp[j%2][1]+dp[(j-1)%2][1])%mod;
dp[j%2][2]=(dp[j%2][2]+dp[(j-1)%2][1])%mod;
dp[j%2][0]=(dp[j%2][0]+dp[(j-1)%2][2])%mod;
dp[j%2][2]=(dp[j%2][2]+dp[(j-1)%2][2])%mod;
}
if (last=='c')
{
dp[j%2][0]=(dp[(j-1)%2][2])%mod;
dp[j%2][2]=(dp[(j-1)%2][2])%mod;
 
}
 
}
last=f[j];
dp[(j-1)%2][0]=0;
dp[(j-1)%2][1]=0;
dp[(j-1)%2][2]=0;
dp[(j-1)%2][3]=0;
}
 
 
 
if (n%2==1)

{int   ans=0,k;
for(k=0;k<3;k++) 
{
ans+=dp[0][k];
ans%=mod;
}
printf("%d\n",ans); 
 
}
else
{
int   ans=0,k;
for(k=0;k<3;k++) 
{
ans+=dp[1][k];
ans%=mod;
}
printf("%d\n",ans); 
 


}


}
return 0;
}
*/

你可能感兴趣的:(Problem(M34):Abc Strings)