Skip to content

Instantly share code, notes, and snippets.

@sturgle
Created February 15, 2015 09:36
Show Gist options
  • Save sturgle/af490cfad44910407774 to your computer and use it in GitHub Desktop.
Save sturgle/af490cfad44910407774 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <sstream>
#include <utility>
#include <set>
using namespace std;
vector<int> cnt;
vector<int> arr;
vector<string> parse(vector<char> &sepArr, char esc, string &s) {
set<char> sep;
for (int i = 0; i < sepArr.size(); i++) {
sep.insert(sepArr[i]);
}
vector<string> result;
// assume valid input
string tmp;
for (int i = 0; i < s.size(); ) {
if (sep.find(s[i]) != sep.end()) {
result.push_back(tmp);
tmp = "";
i++;
} else if (s[i] == esc) {
tmp += s[i+1];
i += 2;
} else {
tmp += s[i];
i++;
}
}
result.push_back(tmp);
return result;
}
// "a,a"
// "a,"
// ",a"
// ","
// ",,"
// "a\,a,a"
// "a,|a"
bool test(string &s) {
vector<char> sep;
sep.push_back(',');
sep.push_back('|');
char esc = '\\';
vector<string> vec = parse(sep, esc, s);
cout << vec.size() << " : ";
for (int i = 0; i < vec.size(); i++) {
cout << '"' << vec[i] << '"';
if (i != vec.size() - 1) {
cout << " , ";
} else {
cout << endl;
}
}
}
int main() {
while (true) {
string s;
cin >> s;
test(s);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment