I did two questions yesterday, and I felt quite confused. But today I feel like remembering my efforts.
The first method is poj 3630 phone list.
A basic trie tree, but because of the dynamic build-up, it was just the beginning of TLE. Finally, the static storage method is used.
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; const int maxn = 100010; struct n {int next [11]; bool sign ;} node [maxn]; string s; int sum; bool add () {int Len = S. length (); int root = 0; For (INT I = 0; I <Len; I ++) {int TMP = s [I]-'0 '; if (node [root]. next [TMP]) {root = node [root]. next [TMP]; if (I = len-1) return 1; if (node [root]. sign) return 1;} else {node [root]. next [TMP] = sum; root = sum ++;} if (I = len-1) node [root]. sign = 1;} return 0;} int main () {int t; CIN> T; while (t --) {sum = 1; bool flag = 1; memset (node, 0, sizeof (node); int N; CIN> N; For (INT I = 0; I <n; I ++) {CIN> S; If (FLAG) if (add () Flag = 0;} If (FLAG) cout <"yes" <Endl; else cout <"no" <Endl ;}}
The second is poj 3067 Japan.
Is a tree array. It is also very simple, but it is also because of a very low-level mistake that caused Wa and TLE a few times.
During the final debugging, we found that the tree array was updated. If the input parameter is a negative number, it would be an endless loop. Then we suddenly realized that the negative number would eventually be 0, and then it would die.
#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <cstring>using namespace std;const int maxn = 1010;struct N{ int f, l;} node[maxn * maxn];int sum[maxn];int n, m, k;vector<int> p;bool cmp(N a, N b){ return a.f > b.f;}void insert(int t){ while(t <= m) { sum[t] += 1; t += t & (-t); }}int findsum(int t){ t--; int ret = 0; while(t > 0) { ret += sum[t]; t -= t & (-t); } return ret;}void print(){ for(int i = 1; i <= m; i++) cout << i << " " << sum[i] << endl;}int main(){ int t; cin >> t; for(int T = 0; T < t; T++) { memset(sum, 0, sizeof(sum)); cin >> n >> m >> k; for(int i = 0; i < k; i++) scanf("%d%d", &node[i].f, &node[i].l); sort(node, node + k, cmp); int tt = 0; while(node[tt].f == node[0].f) { insert(node[tt].l); tt++; } long long ans = 0; for(int i = tt; i < k; i++) { if(node[i].f == node[i - 1].f) { ans += findsum(node[i].l); p.push_back(node[i].l); } else { for(int j = 0; j < p.size(); j++) insert(p[j]); p.clear(); ans += findsum(node[i].l); p.push_back(node[i].l); } } p.clear(); printf("Test case %d: %lld\n", T + 1, ans); }}