-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
1,233 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include <cstdio> | ||
#include <string> | ||
std::string read() { | ||
std::string str; | ||
int c; | ||
do | ||
c = getchar(); | ||
while(c < 'a' || c > 'z'); | ||
while('a' <= c && c <= 'z') { | ||
str.push_back(c); | ||
c = getchar(); | ||
} | ||
return str; | ||
} | ||
const int size = 360005; | ||
struct Node { | ||
int ch[26], fail; | ||
} T[size]; | ||
int icnt = 0; | ||
int insert() { | ||
int c, p = 0; | ||
do | ||
c = getchar(); | ||
while(c < 'a' || c > 'z'); | ||
while('a' <= c && c <= 'z') { | ||
int& v = T[p].ch[c - 'a']; | ||
if(!v) | ||
v = ++icnt; | ||
p = v; | ||
c = getchar(); | ||
} | ||
return p; | ||
} | ||
int q[size]; | ||
void cook() { | ||
int b = 0, e = 0; | ||
for(int i = 0; i < 26; ++i) | ||
if(T[0].ch[i]) | ||
q[e++] = T[0].ch[i]; | ||
while(b != e) { | ||
int u = q[b++]; | ||
for(int i = 0; i < 26; ++i) { | ||
int& v = T[u].ch[i]; | ||
if(v) { | ||
T[v].fail = T[T[u].fail].ch[i]; | ||
q[e++] = v; | ||
} else | ||
v = T[T[u].fail].ch[i]; | ||
} | ||
} | ||
} | ||
int cnt[size], last[size]; | ||
void match(int id, const std::string& str) { | ||
int p = 0; | ||
for(int i = 0; i < str.size(); ++i) { | ||
int ch = str[i] - 'a'; | ||
p = T[p].ch[ch]; | ||
int cp = p; | ||
while(cp) { | ||
if(last[cp] != id) | ||
last[cp] = id, ++cnt[cp]; | ||
cp = T[cp].fail; | ||
} | ||
} | ||
} | ||
int endPos[60005]; | ||
std::string P[10005]; | ||
int main() { | ||
int n, q; | ||
scanf("%d%d", &n, &q); | ||
for(int i = 1; i <= n; ++i) | ||
P[i] = read(); | ||
for(int i = 1; i <= q; ++i) | ||
endPos[i] = insert(); | ||
cook(); | ||
for(int i = 1; i <= n; ++i) | ||
match(i, P[i]); | ||
for(int i = 1; i <= q; ++i) | ||
printf("%d\n", cnt[endPos[i]]); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include <algorithm> | ||
#include <cmath> | ||
#include <cstdio> | ||
#include <cstring> | ||
int read() { | ||
int res = 0, c; | ||
do | ||
c = getchar(); | ||
while(c < '0' || c > '9'); | ||
while('0' <= c && c <= '9') { | ||
res = res * 10 + c - '0'; | ||
c = getchar(); | ||
} | ||
return res; | ||
} | ||
typedef long long Int64; | ||
#define asInt64 static_cast<Int64> | ||
const int size = 100005; | ||
int A[size], B[size], C[size]; | ||
Int64 cans; | ||
void add(int pos, Int64& ans) { | ||
int col = A[pos]; | ||
Int64 val = asInt64(++C[col]) * B[col]; | ||
if(val > ans) | ||
ans = val; | ||
} | ||
Int64 query(int l, int r) { | ||
Int64 ans = cans; | ||
for(int i = l; i <= r; ++i) | ||
add(i, ans); | ||
for(int i = l; i <= r; ++i) | ||
--C[A[i]]; | ||
return ans; | ||
} | ||
struct Queue { | ||
int l, r, bid, id; | ||
bool operator<(const Queue& rhs) const { | ||
return bid == rhs.bid ? r < rhs.r : | ||
bid < rhs.bid; | ||
} | ||
} Q[size]; | ||
Int64 ans[size]; | ||
int main() { | ||
int n = read(); | ||
int q = read(); | ||
for(int i = 1; i <= n; ++i) | ||
A[i] = read(); | ||
memcpy(B + 1, A + 1, sizeof(int) * n); | ||
std::sort(B + 1, B + n + 1); | ||
int siz = std::unique(B + 1, B + n + 1) - (B + 1); | ||
for(int i = 1; i <= n; ++i) | ||
A[i] = std::lower_bound(B + 1, B + siz + 1, | ||
A[i]) - | ||
B; | ||
int bsiz = sqrt(n) + 1; | ||
for(int i = 1; i <= q; ++i) { | ||
Q[i].l = read(); | ||
Q[i].r = read(); | ||
Q[i].id = i; | ||
Q[i].bid = Q[i].l / bsiz; | ||
} | ||
std::sort(Q + 1, Q + q + 1); | ||
Q[0].bid = -1; | ||
int bend, cr; | ||
for(int i = 1; i <= q; ++i) { | ||
if(Q[i].bid != Q[i - 1].bid) { | ||
memset(C + 1, 0, sizeof(int) * siz); | ||
cans = 0; | ||
cr = (Q[i].bid + 1) * bsiz; | ||
bend = cr - 1; | ||
} | ||
while(cr <= Q[i].r) | ||
add(cr++, cans); | ||
ans[Q[i].id] = | ||
query(Q[i].l, std::min(bend, Q[i].r)); | ||
} | ||
for(int i = 1; i <= q; ++i) | ||
printf("%lld\n", ans[i]); | ||
return 0; | ||
} |
Oops, something went wrong.