interviews/training->training
This commit is contained in:
121
puzzles/training/a_bit_more_complex_wildcard_match.cpp
Normal file
121
puzzles/training/a_bit_more_complex_wildcard_match.cpp
Normal file
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I."
|
||||
VIM: let b:wcppflags="/O2 /EHsc /DWIN32"
|
||||
VIM-: let b:argv=""
|
||||
*/
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <list>
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
#include <deque>
|
||||
#include <queue>
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <bitset>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
#include <climits>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <numeric>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* Complete the function below.
|
||||
*/
|
||||
bool match(const string& pattern, const string& string) {
|
||||
|
||||
auto s = string.begin();
|
||||
auto p = pattern.begin();
|
||||
|
||||
const std::string::const_iterator empty;
|
||||
auto prev_p = empty;
|
||||
auto prev_s = s;
|
||||
while (s != string.end()) {
|
||||
while ( p != pattern.end() && *p == '*' ) {
|
||||
p++;
|
||||
prev_p = p;
|
||||
prev_s = s;
|
||||
}
|
||||
if ( p == pattern.end() ) {
|
||||
if ( prev_p == p ){
|
||||
return true;
|
||||
} else if ( prev_p != empty ) {
|
||||
p = prev_p;
|
||||
s = ++prev_s;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if ( *s == *p ){
|
||||
s++;
|
||||
p++;
|
||||
}
|
||||
else if ( *p == '?' ){
|
||||
s++;
|
||||
p++;
|
||||
}
|
||||
else if ( prev_p != empty ) {
|
||||
p = prev_p;
|
||||
s = ++prev_s;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
while ( *p == '*' ) {
|
||||
p++;
|
||||
}
|
||||
return p == pattern.end();
|
||||
}
|
||||
|
||||
|
||||
void test( const std::string& str, const std::string& pattern ){
|
||||
bool res = match(pattern, str);
|
||||
std::cout << str << " - " << pattern << " -> " << res <<std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
#if 1
|
||||
test( "", "" );
|
||||
test( "", "*" );
|
||||
test( "", "?" );
|
||||
test( "a", "?" );
|
||||
test( "abc", "abc" );
|
||||
test( "aaaaabc", "*abc" );
|
||||
test( "aaaaabc", "a*bc" );
|
||||
test( "aaaaabc", "*abc*" );
|
||||
test( "abc", "*abc*" );
|
||||
test( "abc", "****abc*" );
|
||||
test( "abcabaabcabacabc", "a*ba*c" );
|
||||
test( "abcabcabcabccabc", "a*b*cc" );
|
||||
test( "abcabcabcabccabc", "a*b*c?" );
|
||||
test( "abcabcabcabccabc", "a*b*b?*" );
|
||||
test( "abcabcabcabccabc", "*a*?*c*" );
|
||||
test( "abcabcabcabccabc", "****a****??????*c******" );
|
||||
#else
|
||||
ofstream fout(getenv("OUTPUT_PATH"));
|
||||
bool res;
|
||||
string _pattern;
|
||||
getline(cin, _pattern);
|
||||
|
||||
string _string;
|
||||
getline(cin, _string);
|
||||
|
||||
res = match(_pattern, _string);
|
||||
fout << res << endl;
|
||||
|
||||
fout.close();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user