1 UVALive 3942 Remember the Word
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <stack> 9 #include <map> 10 #include <set> 11 #include <cmath> 12 #include <cctype> 13 #include <ctime> 14 15 using namespace std; 16 17 #define REP(i, n) for (int i = 0; i < (n); ++i) 18 #define eps 1e-9 19 20 typedef long long ll; 21 typedef pair<int, int> pii; 22 23 const int INF = 0x7fffffff; 24 const int maxn = 4e5 + 10; 25 const int mod = 20071027; 26 char str[maxn], str_t[110]; 27 int n, sz, Case = 0, ans; 28 int ch[maxn][26], val[maxn], len[maxn], dp[maxn]; 29 30 inline void init() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); } 31 inline int get_id(char c) { return c - 'a'; } 32 void insert(char *s, int _val) { 33 int u = 0, len_t = strlen(s), id; 34 for (int i = 0; i < len_t; ++i) { 35 id = get_id(s[i]); 36 if (!ch[u][id]) { 37 memset(ch[sz], 0, sizeof(ch[sz])); 38 val[sz] = 0; ch[u][id] = sz++; 39 } 40 u = ch[u][id]; 41 } 42 val[u] = _val; 43 } 44 void find_prefixs(int pos, int len_t) { 45 int u = 0, id; 46 for (int i = pos; i < len_t; ++i) { 47 id = get_id(str[i]); 48 if (!ch[u][id]) { break; } 49 u = ch[u][id]; 50 if (val[u] != 0) { dp[pos] += dp[pos + len[val[u]]]; dp[pos] %= mod; } 51 } 52 } 53 54 55 int main() { 56 #ifdef __AiR_H 57 freopen("in.txt", "r", stdin); 58 // freopen("out.txt", "w", stdout); 59 #endif // __AiR_H 60 while (scanf("%s", str) != EOF) { 61 scanf("%d", &n); ans = 0; init(); 62 for (int i = 1; i <= n; ++i) { 63 scanf("%s", str_t); insert(str_t, i); len[i] = strlen(str_t); 64 } 65 memset(dp, 0, sizeof(dp)); 66 int len_t = strlen(str); dp[len_t] = 1; 67 for (int i = len_t - 1; i >= 0; --i) { 68 find_prefixs(i, len_t); 69 } 70 printf("Case %d: %d ", ++Case, dp[0]); 71 } 72 #ifdef __AiR_H 73 printf("Time used = %.2fs ", (double)clock() / CLOCKS_PER_SEC); 74 #endif // __AiR_H 75 return 0; 76 }
2 Uva 11732 "strcmp()" Anyone?
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <stack> 9 #include <map> 10 #include <set> 11 #include <cmath> 12 #include <cctype> 13 #include <ctime> 14 15 using namespace std; 16 17 #define REP(i, n) for (int i = 0; i < (n); ++i) 18 #define eps 1e-9 19 20 typedef long long ll; 21 typedef pair<int, int> pii; 22 23 const int INF = 0x7fffffff; 24 const int maxn = 4e6 + 10; 25 int head[maxn], Next[maxn], tot[maxn]; 26 char ch[maxn], str_t[1010]; 27 int sz, n, Case = 0; 28 ll ans = 0; 29 30 void init() { sz = 1; tot[0] = head[0] = Next[0] = 0; } 31 void insert(char *s) { 32 int u = 0, v, len_t = strlen(s); 33 bool found; ++tot[0]; 34 for (int i = 0; i <= len_t; ++i) { 35 found = false; 36 for (v = head[u]; v != 0; v = Next[v]) { 37 if (ch[v] == s[i]) { found = true; break; } 38 } 39 if (!found) { 40 v = sz++; tot[v] = 0; ch[v] = s[i]; 41 Next[v] = head[u]; head[u] = v; head[v] = 0; 42 } 43 u = v; ++tot[u]; 44 } 45 } 46 void dfs(int u, int depth) { 47 if (!head[u]) { ans += 1ll * tot[u] * (tot[u] - 1) * depth; return; } 48 ll sum = 0; 49 for (int v = head[u]; v != 0; v = Next[v]) { 50 sum += 1ll * tot[v] * (tot[u] - tot[v]); 51 } 52 ans += sum / 2 * (2 * depth + 1); 53 for (int v = head[u]; v != 0; v = Next[v]) { 54 dfs(v, depth + 1); 55 } 56 } 57 58 59 int main() { 60 #ifdef __AiR_H 61 freopen("in.txt", "r", stdin); 62 // freopen("out.txt", "w", stdout); 63 #endif // __AiR_H 64 while (scanf("%d", &n) && n) { 65 init(); 66 while (n--) { scanf("%s", str_t); insert(str_t); } 67 ans = 0; dfs(0, 0); 68 printf("Case %d: %lld ", ++Case, ans); 69 } 70 #ifdef __AiR_H 71 printf("Time used = %.2fs ", (double)clock() / CLOCKS_PER_SEC); 72 #endif // __AiR_H 73 return 0; 74 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 #include <stack> 9 #include <map> 10 #include <set> 11 #include <cmath> 12 #include <cctype> 13 #include <ctime> 14 15 using namespace std; 16 17 #define REP(i, n) for (int i = 0; i < (n); ++i) 18 #define eps 1e-9 19 20 typedef long long ll; 21 typedef pair<int, int> pii; 22 23 const int INF = 0x7fffffff; 24 const int maxn = 1e6; 25 int T, n, sz, ans; 26 char str_t[210]; 27 int ch[maxn][2], cnt[maxn]; 28 29 inline void init() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); } 30 inline int get_id(char c) { return c - '0'; } 31 void insert(char *s) { 32 int u = 0, len_t = strlen(s), id; 33 for (int i = 0; i < len_t; ++i) { 34 id = get_id(s[i]); 35 if (!ch[u][id]) { 36 memset(ch[sz], 0, sizeof(ch[sz])); 37 cnt[sz] = 1; ch[u][id] = sz++; u = ch[u][id]; 38 } else { 39 ++cnt[ch[u][id]]; u = ch[u][id]; 40 } 41 } 42 } 43 void dfs(int u, int depth) { 44 if (u == 0 && depth != 0) { return; } ans = max(ans, cnt[u] * depth); 45 for (int i = 0; i < 2; ++i) { 46 dfs(ch[u][i], depth + 1); 47 } 48 } 49 50 51 int main() { 52 #ifdef __AiR_H 53 freopen("in.txt", "r", stdin); 54 // freopen("out.txt", "w", stdout); 55 #endif // __AiR_H 56 scanf("%d", &T); 57 while (T--) { 58 scanf("%d", &n); init(); ans = 0; 59 for (int i = 0; i < n; ++i) { 60 scanf("%s", str_t); insert(str_t); 61 } 62 dfs(0, 0); printf("%d ", ans); 63 } 64 #ifdef __AiR_H 65 printf("Time used = %.2fs ", (double)clock() / CLOCKS_PER_SEC); 66 #endif // __AiR_H 67 return 0; 68 }