Untitled
Anonymous
Plain Text
10/25/2025 1:47 PM
4.8 KB
19
Indexable
Never
#include <bits/stdc++.h>
using namespace std;
struct DayData {
unordered_set<string> ips;
unordered_set<string> agents;
unordered_set<string> sessions;
vector<pair<int,string>> pdfs; // (pdf number, timestamp)
};
string monthToNum(const string &mon) {
static unordered_map<string,string> m = {
{"Jan","01"},{"Feb","02"},{"Mar","03"},{"Apr","04"},{"May","05"},{"Jun","06"},
{"Jul","07"},{"Aug","08"},{"Sep","09"},{"Oct","10"},{"Nov","11"},{"Dec","12"}
};
return m[mon];
}
// convert [07/Oct/2025:14:26:41] → 2025-10-07 and keep timestamp
pair<string,string> parseDate(const string &d) {
int dd = stoi(d.substr(1,2));
string mon = d.substr(4,3);
string yyyy = d.substr(8,4);
string hhmmss = d.substr(13,8);
ostringstream day;
day << yyyy << "-" << monthToNum(mon) << "-" << setw(2) << setfill('0') << dd;
return {day.str(), hhmmss};
}
bool isPdf(const string &req, int &num) {
smatch m;
regex r("/document/(\\d+)\\.pdf");
if (regex_search(req, m, r)) {
num = stoi(m[1]);
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string line;
if (!getline(cin, line)) return 0;
// --- parse rules ---
unordered_map<string,int> rules;
{
regex r("(agent|ip|pdf|session|crawl)=(\\d+)");
smatch m;
string s = line;
while (regex_search(s, m, r)) {
rules[m[1]] = stoi(m[2]);
s = m.suffix();
}
}
// --- parse header ---
getline(cin, line);
vector<string> headers;
stringstream ss(line);
string item;
while (getline(ss, item, ',')) {
string trimmed;
for (char c : item) if (!isspace(c)) trimmed += c;
headers.push_back(trimmed);
}
unordered_map<string,int> idx;
for (int i=0; i<(int)headers.size(); i++) idx[headers[i]] = i;
// --- parse logs ---
map<string, map<string, DayData>> data; // user -> date -> info
regex token_re(R"("[^"]*"|\[[^\]]*\]|\S+)");
while (getline(cin, line)) {
vector<string> parts;
for (sregex_iterator it(line.begin(), line.end(), token_re), end; it != end; ++it)
parts.push_back(it->str());
if ((int)parts.size() < (int)headers.size()) continue;
auto get = [&](string key) -> string {
string val = parts[idx[key]];
if (!val.empty() && val.front()=='"') val = val.substr(1, val.size()-2);
if (!val.empty() && val.front()=='[') val = val.substr(1, val.size()-2);
return val;
};
string status = get("HTTPStatus");
if (status != "200") continue;
string id = get("Id");
if (id == "-") continue;
string date = get("Date");
auto [daykey, timekey] = parseDate("[" + date + "]");
string req = get("Request");
string ip = get("ClientIP");
string agent = get("UserAgent");
string sess = get("SessionCookie");
DayData &d = data[id][daykey];
d.ips.insert(ip);
d.agents.insert(agent);
d.sessions.insert(sess);
int pdfnum;
if (isPdf(req, pdfnum))
d.pdfs.push_back({pdfnum, date});
}
// --- evaluate abuses ---
vector<pair<string,string>> out;
for (auto &userpair : data) {
const string &uid = userpair.first;
for (auto &daypair : userpair.second) {
DayData &d = daypair.second;
if (rules.count("ip") && (int)d.ips.size() >= rules["ip"])
out.push_back({uid, "ip=" + to_string(d.ips.size())});
if (rules.count("agent") && (int)d.agents.size() >= rules["agent"])
out.push_back({uid, "agent=" + to_string(d.agents.size())});
if (rules.count("session") && (int)d.sessions.size() >= rules["session"])
out.push_back({uid, "session=" + to_string(d.sessions.size())});
if (rules.count("pdf") && (int)d.pdfs.size() >= rules["pdf"])
out.push_back({uid, "pdf=" + to_string(d.pdfs.size())});
if (rules.count("crawl") && !d.pdfs.empty()) {
sort(d.pdfs.begin(), d.pdfs.end(),
[](auto &a, auto &b){ return a.second < b.second; });
int best=1, cur=1;
for (int i=1; i<(int)d.pdfs.size(); i++) {
if (d.pdfs[i].first == d.pdfs[i-1].first + 1)
cur++, best = max(best, cur);
else cur = 1;
}
if (best >= rules["crawl"])
out.push_back({uid, "crawl=" + to_string(best)});
}
}
}
if (out.empty()) {
cout << "N/A\n";
return 0;
}
sort(out.begin(), out.end());
for (auto &p : out)
cout << p.first << " " << p.second << "\n";
}
Editor is loading...
Leave a Comment