Holding Bin-Laden Captive!
Problem Description
We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But recently, it is reported that he hides in Hang Zhou of China! 阅读全文 »
Problem Description
We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But recently, it is reported that he hides in Hang Zhou of China! 阅读全文 »
Problem Description
Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?
Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
Output
In this problem, you have to output the translation of the history book.
Sample Input
START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END
Sample Output
hello, i'm from mars.
i like earth!
Hint Huge input, scanf is recommended.
Author
Ignatius.L
题目类型:trie树
算法分析:将外文单词插入到Trie树中,每个终止节点额外存储一个对应的英文单词,然后在文本中找到一个字符串ss,然后判断其是否在Trie树中,不在就直接输出ss,否则就输出这个终止节点所对应的英文单词
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 1e9 + 7; const int maxn = 26; struct Trie { Trie *next[maxn]; char ss[maxn]; Trie () { memset (next, 0, sizeof (next)); memset (ss, 0, sizeof (ss)); } }; Trie *root; void Init () { root = new Trie (); } void Insert (char *s, char *bb) { if (!s[0]) return ; Trie *p = root; for (int i = 0; s[i]; i++) { if (!p -> next[s[i]-'a']) { p -> next[s[i]-'a'] = new Trie; } p = p -> next[s[i]-'a']; } strcpy (p -> ss, bb); } char res[maxn+66]; bool Query (char *s) { if (!s[0]) return false; Trie *p = root; for (int i = 0; s[i]; i++) { if (!p -> next[s[i]-'a']) return false; p = p -> next[s[i]-'a']; } if (p -> ss[0]) { strcpy (res, p -> ss); return true; } return false; } void DelTrie (Trie *t) { if (!t) return ; for (int i = 0; i < maxn; i++) { if (t -> next[i]) DelTrie (t -> next[i]); } delete t; } int main() { // freopen ("aaa.txt", "r", stdin); char aa[maxn+3000], bb[maxn+3000]; scanf ("%s", aa); Init (); while (scanf ("%s", aa) != EOF) { if (!strcmp (aa, "END")) break; scanf ("%s", bb); Insert (bb, aa); } scanf ("%s", aa); gets (aa); while (gets (aa)) { if (!strcmp (aa, "END")) break; char temp[maxn]; int len; for (int i = 0; aa[i]; ) { if (aa[i] >= 'a' && aa[i] <= 'z') { len = 0; while (aa[i] >= 'a' && aa[i] <= 'z') { temp[len++] = aa[i]; i++; } temp[len] = 0; if (Query (temp)) printf ("%s", res); else printf ("%s", temp); } else { printf ("%c", aa[i]); i++; } } puts (""); } return 0; } |
Problem Description
"Well, it seems the first problem is too easy. I will let you know how foolish you are later." feng5166 says.
"The second problem is, given an positive integer N, we define an equation like this:
N=a[1]+a[2]+a[3]+...+a[m];
a[i]>0,1<=m<=N;
My question is how many different equations you can find for a given N.
For example, assume N is 4, we can find:
4 = 4;
4 = 3 + 1;
4 = 2 + 2;
4 = 2 + 1 + 1;
4 = 1 + 1 + 1 + 1;
so the result is 5 when N is 4. Note that "4 = 3 + 1" and "4 = 1 + 3" is the same in this problem. Now, you do it!"
Input
The input contains several test cases. Each test case contains a positive integer N(1<=N<=120) which is mentioned above. The input is terminated by the end of file.
Output
For each test case, you have to output a line contains an integer P which indicate the different equations you have found.
Sample Input
4
10
20
Sample Output
5
42
627
Author
Ignatius.L
题目类型:整数的拆分
算法分析:直接使用普通母函数将其转换成求解多项式乘法的问题,然后直接输出coeff[n]的值即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 10000 + 7; const int maxn = 100 + 66; int coeff[maxn], temp[maxn]; void Init () { memset (coeff, 0, sizeof (coeff)); memset (temp, 0, sizeof (temp)); } void GenerFunction (long long expnum) { for (long long i = 0; i <= expnum * 1; i += 1) coeff[i] = 1; for (long long i = 2; i <= expnum; i++) { for (long long j = 0; j <= expnum; j++) { for (long long k = 0; k + j <= expnum; k += i) temp[k+j] += coeff[j]; } for (long long j = 0; j <= expnum; j++) { coeff[j] = temp[j]; temp[j] = 0; } } } int main() { // ifstream cin ("aaa.txt"); long long n; while (cin >> n) { Init (); GenerFunction (n); cout << coeff[n] << endl; } return 0; } |
Problem Description
JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. 阅读全文 »
Problem Description
As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway. 阅读全文 »
Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
Sample Input
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
Sample Output
13.3333
1.500
Author
CHEN, Yue
Source
题目类型:贪心
算法分析:直接算出每一种物品的价值/花费,然后按其从大到小排序。最后对于给定的背包的容量,尽量将单位价值高的物品放入。注意本题的坑点是精度!!!!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 7; const int maxn = 66600 + 66; struct Node { double v, w; double avr; }; Node ans[maxn]; bool Cmp (Node a, Node b) { return a.avr > b.avr; } int main() { // ifstream cin ("aaa.txt"); int n, m; while (cin >> m >> n) { if (m == -1 && n == -1) break; int i; for (i = 0; i < n; i++) { cin >> ans[i].v >> ans[i].w; ans[i].avr = ans[i].v * 10.0 / ans[i].w / 10; } sort (ans, ans + n, Cmp); double sum = 0; i = 0; while (i < n && m - ans[i].w >= 1e-5) { sum += ans[i].v; m -= ans[i].w; i++; } if (i < n) sum += ans[i].avr * m; printf ("%.3lf\n", sum); } return 0; } |
Problem Description
A number sequence is defined as follows:
f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.
Given A, B, and n, you are to calculate the value of f(n). 阅读全文 »
Problem Description
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.
This year, they decide to leave this lovely job to you.
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.
A test case with N = 0 terminates the input and this test case is not to be processed.
Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
0
Sample Output
red
pink
Author
WU, Jiazhi
Source
题目类型:map/Trie树
算法分析:直接建立一个映射字符串的数据结构,然后统计次数即可,这里分别使用STL中的map和Trie树
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 60000 + 66; int main() { // ifstream cin ("aaa.txt"); // freopen ("aaa.txt", "r", stdin); int n; while (cin >> n) { if (n == 0) break; map <string, int> cnt; string s; int maxval = -INF; string res; for (int i = 1; i<= n; i++) { cin >> s; cnt[s]++; if (maxval < cnt[s]) { maxval = cnt[s]; res = s; } } cout << res << endl; } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 26; const int nodemaxn = 15000; int maxval; struct Trie { int next[nodemaxn][maxn], last[nodemaxn], cnt[nodemaxn]; int root , len; void Init () { len = 0; root = newnode (); } int newnode () { for (int i = 0; i < maxn; i++) next[len][i] = -1; last[len] = 0, cnt[len] = 0; len++; return len - 1; } void Insert (char *s) { int p = root; for(int i = 0; s[i]; i++) { if(next[p][s[i]-'a'] == -1) next[p][s[i]-'a'] = newnode (); else { p = next[p][s[i]-'a']; } } last[p] = 1; cnt[p]++; } int Query (char *s) { if (!s[0]) return 0; int p = root; for (int i = 0; s[i]; i++) { if (next[p][s[i]-'a'] == -1) return 0; p = next[p][s[i]-'a']; } if (last[p]) return cnt[p]; } }; Trie aa; char ss[1111][maxn]; int main() { // ifstream cin ("aaa.txt"); // freopen ("aaa.txt", "r", stdin); int n; while (cin >> n) { if (n == 0) break; aa.Init (); for (int i = 1; i <= n; i++) { cin >> ss[i]; aa.Insert (ss[i]); } maxval = -INF; char sss[maxn]; for (int i = 1; i <= n; i++) { if (maxval < aa.Query(ss[i])) { maxval = aa.Query (ss[i]); strcpy(sss, ss[i]); } } cout << sss << endl; } return 0; } |