L1-071 Past life archives (20 points)

Insert picture description here

Such funny fortune-telling applets are often encountered in the online world. The principle of implementation is very simple. Just design a few questions and choose a path in the judgment tree according to the player's answer to each question (as shown in the figure below). The conclusion is The node corresponding to the end of the path.

path.jpg

Now we number the conclusions sequentially from left to right, starting with 1. It is assumed that the answers are simply "yes" or "no", and that the answer "yes" corresponds to the path to the left, and the answer "no" corresponds to the path to the right. Given a series of answers from the player, please return the number of the conclusion he got.

Input format:
Input the first line to give two positive integers: N (≤30) is the number of questions the player has to answer in a test; M (≤100) is the number of players.

Following M rows, each row gives N answers from the player in sequence. Here y is used for "yes" and n is used for "no".

Output format:
For each player, output the number of the corresponding conclusion in one line.

Input sample:
3 4
yny
nyy
nyn
yyn
Output sample:
3
5
6
2

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    
    
	int n,m,ans;
	string s;
	cin>>n>>m;
	while(m--)
	{
    
    
		ans=1;
		cin>>s;
		for(int i=0;i<n;i++)
		 if(s[i]=='n') 
		  ans+=pow(2,n-i-1); 
		cout<<ans<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43540515/article/details/112978837