Full Question — IP Request Validation System You ...
創建於:2026年10月7日
使用 GPT-5.6 Thinking 回答 Chat01
創建於:2026年10月7日
使用 GPT-5.6 Thinking 回答 Chat01
Full Question — IP Request Validation System
You are given an IP request validation system. The system has two rules for validating requests:
Blacklisted IP patterns
Blacklisted regex patterns can contain:
The wildcard * represents one or more characters.
IPs are formatted as strings of four integers separated by three dots:
<number>.<number>.<number>.<number>
Example
n = 3
blacklisted_ips = ["m*.", "123.", "34.*"]
q = 7
requests = [
"123.1.23.34",
"121.1.23.34",
"121.1.23.34",
"34.1.23.34",
"121.1.23.34",
"12.1.23.34",
"121.1.23.34"
]
The requests arrive at times 1 through 7 respectively.
Request processing
Time Request IP Matching Blacklisted IP Number of unblocked requests from this IP in last 5 sec Blocked
1 123.1.23.34 123.* 0 1
2 121.1.23.34 - 0 0
3 121.1.23.34 - 1 0
4 34.1.23.34 34.* 0 1
5 121.1.23.34 - 2 1
6 12.1.23.34 - 0 0
7 121.1.23.34 - 1 0
Therefore:
answer = [1, 0, 0, 1, 1, 0, 0]
Important detail
At time 7, the request
121.1.23.34
is not blocked because in the last 5 seconds only the request that came at time 3 is counted.
The request that came at time 5 was blocked, so it is not included in the count of unblocked requests.
Function Description
Complete the function:
vector<int> validateRequests(
vector<string> blacklisted_ips,
vector<string> requests
)
Parameters
Returns
int[]
For each request:
Constraints
1 <= n <= 10
1 <= length of blacklisted_ips[i] <= 15
1 <= q <= 1000
1 <= length of requests[i] <= 15
Sample Input
2
111..255
12.
4
121.3.5.255
12.13.5.255
111.3.5.255
121.3.5.255
Sample Output
0
0
1
0
The third request is blocked because it matches:
111.*.255
All other requests can be processed.
Solved this but passed 13/15
#include <bits/stdc++.h>
using namespace std;
bool match(string s, string p) {
int n = s.size(), m = p.size();
vector<vector<int>> dp(n + 1, vector<int>(m + 1));
textdp[0][0] = 1; for (int i = 0; i <= n; i++) { for (int j = 1; j <= m; j++) { if (p[j - 1] == '*') { if (i > 0) dp[i][j] = dp[i - 1][j] || dp[i - 1][j - 1]; } else if (i > 0 && s[i - 1] == p[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } } } return dp[n][m];
}
vector<int> validateRequests(vector<string> blacklisted_ips, vector<string> requests) {
unordered_map<string, deque<int>> mp;
vector<int> ans;
textfor (int i = 0; i < (int)requests.size(); i++) { string ip = requests[i]; bool blocked = false; for (string p : blacklisted_ips) { if (match(ip, p)) { blocked = true; break; } } if (!blocked) { auto &q = mp[ip]; while (!q.empty() && q.front() <= i - 5) q.pop_front(); if (q.size() >= 2) blocked = true; else q.push_back(i); } ans.push_back(blocked ? 1 : 0); } return ans;
}
int main() {
vector<string> b = {"m*.", "123.", "34.*"};
vector<string> r = {
"123.1.23.34",
"121.1.23.34",
"121.1.23.34",
"34.1.23.34",
"121.1.23.34",
"12.1.23.34",
"121.1.23.34"
};
textvector<int> ans = validateRequests(b, r); for (int x : ans) cout << x << '\n'; return 0;
}
Your rate-limit logic is basically right. The likely hidden-test failure is in the wildcard matcher, so I’m checking its exact * = one or more characters behavior and edge cases rather than changing the 5-second window blindly.
pythondef m1(s,p): n,m=len(s),len(p) dp=[[0]*(m+1) for _ in range(n+1)] dp[0][0]=1 for i in range(n+1): for j in range(1,m+1): if p[j-1]=='*': if i>0: dp[i][j]=dp[i-1][j] or dp[i-1][j-1] elif i>0 and s[i-1]==p[j-1]: dp[i][j]=dp[i-1][j-1] return bool(dp[n][m]) from functools import lru_cache def m2(s,p): @lru_cache(None) def f(i,j): if j==len(p): return i==len(s) if p[j]=='*': # consume one or more return i < len(s) and (f(i+1,j) or f(i+1,j+1)) return i<len(s) and s[i]==p[j] and f(i+1,j+1) return f(0,0) import itertools, random chars='12.' for ls in range(5): for lp in range(5): for s in map(''.join,itertools.product(chars, repeat=ls)): for p in map(''.join,itertools.product(chars+'*', repeat=lp)): if m1(s,p)!=m2(s,p): print("diff",s,p,m1(s,p),m2(s,p)); raise SystemExit print("all same")
I found the likely reason you’re getting 13/15. Your rate-limiting logic is correct; the issue is almost certainly the semantics of *.
The publicly posted versions of this UBS OA problem specify that * matches zero or more characters, not one or more. (LeetCode)
Your current transition:
cppif (p[j - 1] == '*') { if (i > 0) dp[i][j] = dp[i - 1][j] || dp[i - 1][j - 1]; }
forces * to consume at least one character.
For * = zero or more characters, you need:
cppdp[i][j] = dp[i][j - 1] || (i > 0 && dp[i - 1][j]);
The two cases are:
textdp[i][j - 1] -> '*' matches zero characters dp[i - 1][j] -> '*' consumes one more character
So use this:
cppbool match(const string& s, const string& p) { int n = s.size(), m = p.size(); vector<vector<bool>> dp(n + 1, vector<bool>(m + 1, false)); dp[0][0] = true; for (int j = 1; j <= m; j++) { if (p[j - 1] == '*') dp[0][j] = dp[0][j - 1]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (p[j - 1] == '*') { // zero chars OR one/more chars dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; } else if (s[i - 1] == p[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; } } } return dp[n][m]; }
Your validateRequests() can otherwise stay essentially unchanged:
cppvector<int> validateRequests( vector<string> blacklisted_ips, vector<string> requests ) { unordered_map<string, deque<int>> mp; vector<int> ans; for (int i = 0; i < requests.size(); i++) { const string& ip = requests[i]; bool blocked = false; // Rule 1: blacklist for (const string& p : blacklisted_ips) { if (match(ip, p)) { blocked = true; break; } } // Rule 2: rate limiting if (!blocked) { auto& q = mp[ip]; // Requests exactly 5 seconds old are NOT counted. while (!q.empty() && q.front() <= i - 5) q.pop_front(); if (q.size() >= 2) { blocked = true; } else { // Only store UNBLOCKED requests. q.push_back(i); } } ans.push_back(blocked ? 1 : 0); } return ans; }
This part:
cppwhile (!q.empty() && q.front() <= i - 5) q.pop_front();
is correct.
At time 7 (i = 6 zero-based):
texti - 5 = 1
so the request at zero-based index 1 (time 2) gets removed, while the request at index 2 (time 3) remains.
And you're correctly not inserting blocked requests:
cppif (q.size() >= 2) blocked = true; else q.push_back(i);
That's exactly why the blocked request at time 5 doesn't affect time 7.
You have:
textblacklist: 12.* request: 12.13.5.255
Under either “one or more” or “zero or more” wildcard semantics, 12.* matches 12.13.5.255, so that request should be blocked. Thus the sample output you pasted cannot simultaneously be correct with the stated wildcard rules; there's likely a transcription error in that sample.
For the 13/15 issue, however, I'd submit the zero-or-more * version above. That's the semantics found in the available versions of this exact UBS validation problem. (LeetCode)