Central South University, the machine issues - in order to match the character

Central South University, the machine issues - in order to match the character

Problem Description:
We are concerned about the degree of difficulty of the exam, K teacher out of question to have a law on the topic before, it will write down a random string, you can find E, A, S sequence as long as this string, Y four letters, his topic will be relatively simple. You got the string, you tell people it is hard topic.
Input:
data input multiple groups per line, one string (string length does not exceed 1000).
Output:
For each set of input data, output line, corresponding to a requirement of the answer (topic simply outputs easy, it is difficult to output difficult).
The Input the Sample:
EASY
SEoAtSNY
the Sample the Output:
'apologetic'
the Easy

Algorithm idea:
the EASY to compare the character array stored in the array as an auxiliary, provided two pointer i, j, pointer i pointing to a given string, the auxiliary point j array when a match, i and j are incremented simultaneously, If not i is incremented by one, if the given string points i and j do not point to the end of the string at the end of the secondary, the output 'apologetic, if j points to the end of the auxiliary string, the output easy.

#include<iostream>
#include<string>
using namespace std;
bool function(string str) {
	char tmp[4] = { 'E','A','S','Y' };
	int i = 0, j = 0;
	while (i < str.length() && j < 3) {
		if (str[i] == tmp[j]) {
			i++;
			j++;
		}
		i++;
	}
	if (i >= str.length())
		return false;
	else
		return true;
}
int main() {
	string str;
	int count = 2;
	while (count) {
		cout << "输入字符串:";
		cin >> str;
		if (function(str)) {
			cout << "easy" << endl;
		}
		else
			cout << "difficult" << endl;
		count--;
	}
	return 0;
}

Run the test results:
Here Insert Picture Description

Published 47 original articles · won praise 47 · views 1638

Guess you like

Origin blog.csdn.net/weixin_45295612/article/details/105361557