原题链接:
Weather Station
大意:
八个风向,
给一个字符串,求表示的意思有多少个,对1e9+7取模。
Note:
The line in the first example has two solutions:
NEWS can means
{N, E, W, S}and{NE, W, S}.
dp即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mem(s,t) memset(s,t,sizeof(s))
#define D(v) cout<<#v<<" "<<v<<endl
#define inf 0x3f3f3f3f
#define pb push_back
#define pair <int,int> pii
#define LOCAL
const int MAXN =1e5+10;
const int mod=1e9+7;
char a[MAXN];
int dp[MAXN];
int main() {
#ifdef LOCAL
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
scanf("%s",a);
int n=strlen(a);
mem(dp,0);
dp[0]=dp[1]=dp[2]=1;
for(int i=1;i<n;i++){
if(a[i-1]=='N'&&a[i]=='E' || a[i-1]=='N'&&a[i]=='W'){
if(i>=2) dp[i]=(dp[i-2]+dp[i-1])%mod;
else dp[i]=(1+dp[i-1])%mod;
}else if(a[i-1]=='S'&&a[i]=='W' || a[i-1]=='S'&&a[i]=='E'){
if(i>=2) dp[i]=(dp[i-2]+dp[i-1])%mod;
else dp[i]=(1+dp[i-1])%mod;
}else dp[i]=dp[i-1];
}
printf("%d\n",dp[n-1]);
return 0;
}