Problem:
Score : 200 points

Problem Statement

Snuke lives on an infinite two-dimensional plane. He is going on an N-day trip.
At the beginning of Day 1, he is at home. His plan is described in a string S of length N.
On Day i(1 ≦ i ≦ N), he will travel a positive distance in the following direction:

North if the i-th letter of S is N

West if the i-th letter of S is W

South if the i-th letter of S is S

East if the i-th letter of S is E

He has not decided each day's travel distance. Determine whether it is possible to set each day's travel distance so that he will be back at home at the end of Day N.

Constraints

1 ≦ | S | ≦ 1000

S consists of the letters N, W, S, E.

Input

The input is given from Standard Input in the following format:

S

Output

Print Yes if it is possible to set each day's travel distance so that he will be back at home at the end of Day N. Otherwise, print No.

Sample Input 1

SENW

Sample Output 1

Yes

If Snuke travels a distance of 1 on each day, he will be back at home at the end of day 4.

Sample Input 2

NSNNSNSN

Sample Output 2

Yes

Sample Input 3

NNEW

Sample Output 3

No

Sample Input 4

W

Sample Output 4

No

Solution in C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include<bits/stdc++.h>
#define ll long long
#define inf 0x7fffffff
#define MOD 998244353
using namespace std;
const int maxn=100010;
inline ll read(){
	ll x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-')f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return f*x;
}

int n,q,m;
ll a[maxn],b[maxn],c[maxn],ans[maxn],num[maxn];

inline int nex(ll x){
	int l=1,r=m+1,mid;
	while (l<r){
		mid=l+r>>1;
		if (x<c[mid])l=mid+1;else r=mid;
	}
	return l;
}

int main(){
	//freopen("text.in","r",stdin);
	//freopen("text1.out","w",stdout);
	memset(ans,0,sizeof(ans));
	memset(num,0,sizeof(num));
	n=read();q=read();
	for (int i=1;i<=n;++i)a[i]=i;
	for (int i=1;i<=q;++i)b[i]=read();b[0]=n;
	c[m=1]=b[q];
	for (int i=q-1;i>=0;--i)if (b[i]<c[m])c[++m]=b[i];
	num[1]=1;
	for (int i=1;i<m;++i){
		ll x=num[i],y=c[i];int j=i+1;
		while (j<=m){
			num[j]+=y/c[j]*x;
			y%=c[j];
			j=nex(y);
		}
		if (y)ans[y]+=x;
	}
	ans[c[m]]+=num[m];
	for (int i=n-1;i;--i)ans[i]+=ans[i+1];
	for (int i=1;i<=n;++i)printf("%lld\n",ans[i]);
}