UVa 766 - Sum of powers

題目描述

$$\begin{align*} & \sum_{i = 1}^{n} i = \frac{n(n+1)}{2} \\ & \sum_{i = 1}^{n} i^{2} = \frac{n(n+1)(2n+1)}{6} \\ & ... \\ & \sum_{i = 1}^{n} i^{k} = ? \\ \end{align*}$$

求 k < 20 的所有情況,並且把方程式列出。

Solution

當初在推倒$\sum_{i = 1}^{n} i^{2}$ 的時候,採用的方式如下:

首先,升一次觀察:

$$\begin{align*} & (i + 1)^{3} - i^{3} = 3i^{2} + 3i + 1 \end{align*}$$

整理,將$i^{2}$ 移到單獨項

$$\begin{align*} & i^{2} = \frac{1}{3} ((i + 1)^{3} - i^{3} - 3i - 1) \\ & \Rightarrow \sum_{i = 1}^{n} i^{2} = \sum_{i = 1}^{n} \frac{1}{3} ((i + 1)^{3} - i^{3} - 3i - 1) \end{align*}$$

此時特別計算$\sum_{i = 1}^{n} (i + 1)^{3} - i^{2}$,易見答案為$(n+1)^{3} - 1$

最後得到
$$\begin{align*} & \Rightarrow \sum_{i = 1}^{n} i^{2} = \frac{1}{3} ((n+1)^{3} - 1 + \sum_{i = 1}^{n} (-3i-1)) \end{align*}$$

藉由上述的推倒過程,可以擴充至更高維度的計算。

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
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
long long C[25][25] = {};
long long M[25] = {}, A[25][25] = {};
long long llabs(long long v) {
return v < 0 ? -v : v;
}
void init() {
C[0][0] = 1;
for(int i = 1; i < 25; i++) {
C[i][0] = 1;
for(int j = 1; j <= i; j++)
C[i][j] = C[i-1][j] + C[i-1][j-1];
}
// sum of powers
M[0] = 1, A[0][1] = 1; // sigma(k^0) = n
for(int i = 1; i <= 20; i++) {
M[i] = i + 1;
long long val = 1;
for(int j = 0; j <= i - 1; j++)
val = val / __gcd(val, M[j]) * M[j];
M[i] *= val;
// printf("%lld %lld\n", M[i], val);
for(int j = 0; j <= i - 1; j++)
for(int k = 0; k <= j + 1; k++)
A[i][k] -= A[j][k] * val / M[j] * C[i + 1][j];
for(int j = 0; j <= i + 1; j++)
A[i][j] += val * C[i+1][j];
A[i][0] -= val * 1;
long long g = M[i];
for(int j = 0; j <= i + 1; j++)
g = __gcd(g, llabs(A[i][j]));
M[i] /= g;
for(int j = 0; j <= i + 1; j++)
A[i][j] /= g;
}
}
/*
find sigma(i^2)
(i + 1)^3 - (i)^2 = 3*(i)^2 + 3i + 1
=> i^2 = 1/3 * ((i + 1)^3 - (i)^2 - 3i - 1)
=> sigma(i^2) = sigma(1/3 * ((i + 1)^3 - (i)^2 - 3i - 1))
=> = 1/3 (sigma((i + 1)^3 - (i)^2) + sigma(-3i - 1))
=> // sigma((i + 1)^3 - (i)^3) = (n + 1)^3 - 1
=> = 1/3 ((n + 1)^3 - 1 + sigma(-3i - 1))
... so
*/
int main() {
init();
int testcase, n;
scanf("%d", &testcase);
while(testcase--) {
scanf("%d", &n);
printf("%lld", M[n]);
for(int i = n + 1; i >= 0; i--)
printf(" %lld", A[n][i]);
puts("");
if(testcase)
puts("");
}
return 0;
}
Read More +

UVa 12779 - The Largest Circle

題目描述

對於一個平行四邊形,找到平行四邊形內接最大圓面積為何。

並且以分數的方式輸出,保證輸入的四個點一定能構成平行四邊形。

Solution

找到平行四邊形的兩個高中最小得即可。

因此會使用到點線投影方程式,從方程式中可以得知一定能用分數形式輸出,不用特別考慮 -1 的案例輸出。

Read More

UVa 12778 - Minimum Sum

題目描述

對於任一區間 [l, r] 找到一個最小值 a[m] 其中 l <= m <= r,計算

$$\begin{aligned} f(l, r) = \sum_{i = l}^{r} |a[m] - a[i]| \end{aligned}$$

並且把所有 f(l, r) 加總起來。

Solution

要將所有情況加總,因此會有 C(n, 2) 個對。

反過來思考,對於每一個最小值 a[m] 來說,有多少區間會使用到它進行計算。也就是說,找到一個最大的區間 [l, r] 包含 a[m],同時這個區間內的數字皆大於等於 a[m]

則對於 a[m] 而言,會找到一個最大的區間 [l, m, r],會有 (m - l + 1) * (r - m + 1) 個區間會認定 a[m] 是該區間的最小值,則直接計算即可。

但這裡也存有一個很大的問題,會有兩個相同小的數字,特別處理它。

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
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MAXN 65536
#define INF 0x3f3f3f3f
long long sum[MAXN], ssum[MAXN], isum[MAXN], issum[MAXN];
// binary indexed tree
int BIT[MAXN<<1];
int query(int x) {
int ret = 0;
while(x) {
ret = max(ret, BIT[x]);
x -= x&(-x);
}
return ret;
}
void modify(int x, int v, int L) {
while(x <= L) {
BIT[x] = max(BIT[x], v);
x += x&(-x);
}
}
int query2(int x) {
int ret = INF;
while(x) {
ret = min(ret, BIT[x]);
x -= x&(-x);
}
return ret;
}
void modify2(int x, int v, int L) {
while(x <= L) {
BIT[x] = min(BIT[x], v);
x += x&(-x);
}
}
int cases = 0, preA[MAXN], sufA[MAXN], start[MAXN], cover[MAXN<<1];
int main() {
// freopen("in.txt", "r+t", stdin);
// freopen("out2.txt", "w+t", stdout);
int testcase, n, A[MAXN];
scanf("%d", &testcase);
while(testcase--) {
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &A[i]), start[i] = 0, cover[A[i] + MAXN] = -1;
int L = 50000 + MAXN;
for(int i = 0; i <= L; i++)
BIT[i] = 0;
for(int i = 1; i <= n; i++) {
preA[i] = query(A[i] + MAXN);
modify(A[i]+MAXN, i, L);
if(preA[i] <= cover[A[i] + MAXN])
start[i] = cover[A[i] + MAXN] + 1;
cover[A[i] + MAXN] = i;
}
for(int i = 0; i <= L; i++)
BIT[i] = n + 1;
for(int i = n; i >= 1; i--) {
sufA[i] = query2(A[i] + MAXN);
modify2(A[i]+MAXN+1, i, L);
}
for(int i = 1; i <= n; i++)
sum[i] = sum[i-1] + A[i];
for(int i = 1; i <= n; i++)
ssum[i] = ssum[i-1] + sum[i];
isum[n+1] = issum[n+1] = 0;
for(int i = n; i >= 1; i--)
isum[i] = isum[i+1] + A[i];
for(int i = n; i >= 1; i--)
issum[i] = issum[i+1] + isum[i];
long long ret = 0;
for(int i = 1; i <= n; i++) {
long long suma, a, sumb, b;
if(start[i]) {
suma = issum[start[i]] - issum[i], a = i - start[i];
sumb = ssum[sufA[i]-1] - ssum[i], b = sufA[i] - i - 1;
suma -= isum[i] * a;
sumb -= sum[i] * b;
} else {
suma = issum[preA[i]+1] - issum[i], a = i - preA[i] - 1;
sumb = ssum[sufA[i]-1] - ssum[i], b = sufA[i] - i - 1;
suma -= isum[i] * a;
sumb -= sum[i] * b;
}
// printf("index %d\n", i);
// printf("%lld %lld\n", ssum[sufA[i]-1], ssum[i]);
// printf("%lld %lld %lld %lld -- %lld\n", suma, a, sumb, b, (suma - A[i]*a*(a+1)/2) * (b + 1) + (sumb - A[i]*b*(b+1)/2) * (a + 1));
ret += (suma - A[i]*a*(a+1)/2) * (b + 1) + (sumb - A[i]*b*(b+1)/2) * (a + 1);
// long long test = 0;
// for(int j = i; j >= 1; j--) {
// if(A[j] < A[i])
// break;
// for(int k = i; k <= n; k++) {
// if(A[k] < A[i]) break;
// for(int l = j; l <= k; l++)
// test += A[l] - A[i];
// }
// }
// printf("DEBUG %lld\n", test);
}
printf("Case %d: %lld\n", ++cases, ret);
}
return 0;
}
/*
1
5
1 2 3 4 5
10
10
-7 2 -6 7 -9 5 -5 -3 9 3
10
1 -7 9 2 4 7 -10 8 0 0
10
-8 9 4 -8 1 2 -3 2 8 8
10
-3 -4 0 -2 5 9 -3 -4 2 1
10
3 -8 -9 -4 8 -9 -7 6 -10 -9
10
-7 0 -2 8 -6 6 9 -5 -1 -3
10
1 -10 7 -1 7 -10 5 -1 -10 9
10
-2 9 -8 8 -7 0 -1 -8 7 -9
10
-9 2 3 -8 -10 9 7 9 1 3
10
3 5 1 -4 -6 -3 0 2 -5 -5
*/
Read More +

UVa 451 - Poker Solitaire Evaluator

Input

The input will contain several test cases. First line of the input is the an integer which indicate the number of test case followed by a blank line. Each consecutive test case will also separated by a blank line.

Each test case gets 25 cards, 5 cards per line. Each card consists of two characters. The first represents the rank of the card: A',2’, 3',4’, 5',6’, 7',8’, 9',X’, J',Q’, K'. The second represents the suit of the card:S’, H',D’, `C’.

The cards are dealt into a tex2html_wrap_inline44 square. Each row and column is evaluated to determine the highest hand type for which its 5 cards qualify. The hand types, from low to high, are Nothing, Pair, Two Pair, Three of a Kind, Straight, Flush, Full House, Four of a Kind, Straight Flush. A hand qualifies only once, and then only for its highest type. For example, a Four of a Kind does not count as two pair or three of a kind.

Output

For each test case output a list of 9 integers, telling how many hands of each handtype were found. from lowest to highest, being:

Nothing: does not qualify as any of the following. Example: AC, 3H, QS, JD, 7D.

One Pair: contains two cards of the same rank and does not qualify for any of the following. Example: 2C, 3H, 4H, 2H, KD.

Two Pair: contains two cards of one rank and two cards of another and does not qualify for any of the following. Example: 2C, 3H, 4H, 2H, 4D.

Three of a Kind: contains three cards of the same rank and does not qualify for any of the following. Example: QS, KH, 2C, QD, QC.

Straight: the five cards of the hand may be sorted on rank so that an unbroken sequence of 5 ranks is formed and the hand does not qualify for any of the following. There can be cycle through Ace. That is AC, 2H, 4D, 3H, 5S forms a straight, as does JH, XD,QC, KD, AS and QC, KD, AS, 2H, 3D.

Flush: the five cards are all of the same suit and the hand does not qualify for any of the following. Example: 5D, AD, KD, 7D, QD.

Full House: the hand contains three cards of one rank and two cards of another. Example: 3C, QS, QD, 3H, 3S.

Four of a kind: the hand contains four cards of the same rank. Example: AS, AD, AH, 7C, AC.

Straight Flush: the hand meets the criteria for being both a straight and a flush. 

Two consecutive output will separated by a blank line.

For the example below, the five rows evaluate to Straight Flush, Straight, Pair, Flush, Three of a Kind. The Five columns evaluate to Four of A Kind, Full House, Two Pair, Nothing, and Two Pair.

Sample Input

1
2
3
4
5
6
7
1
AS 2S 3S 4S 5S
AC 2H 3H 5C 4C
AH 2D KC KH 5D
AD 3D KD 9D 8D
XH 3C XC XS 8C

Sample Output

1
1, 1, 2, 1, 1, 1, 1, 1, 1

Solution

一張撲克牌有花色(Clubs, Diamonds, Hearts, Spades,分別以C,D,H,S來代表)及點數(2,3,4,5,6,7,8,9,10, jack, queen, king, ace,分別以2,3,4,5,6,7,8,9,X,J,Q,K,A來代表)。有一種常見的撲克牌遊戲叫”梭哈”(就是你在電影賭神中常見的那種遊戲),每個人手上有5張牌,並且依照以下的規則給予每手牌1-9的整數值。注意:每手牌只能有一個值(高的那一個),例如:Four of a kind就不能算為two pairs 或 three of a kind.

  1. Nothing:(中文翻譯:不知道,就是最爛的就是了)沒有以下任何情況就屬於nothing。例如:AH 9D 7C 3H 2D
  2. One pair:(中文翻譯:1對)有2張牌同一個點數,另3張牌皆不同點數。例如:8H 8D KC 5H 2D
  3. Two pairs:(中文翻譯:2對)有2組2張牌同一個點數,另1張牌不同點數。例如:8H 8D 5C 5H 9D
  4. Three of a kind:(中文翻譯:3條)3張牌同一個點數,另2張牌不同點數。例如:8H 8D 8C AH 9D
  5. Straight:(中文翻譯:順子)5張牌的點數為連續的並且可以A連接2成一圈。(所以:點數T J Q K A是順子,A 2 3 4 5是順子,Q K A 2 3也是順子)例如:8H 9H TH JH QD
  6. Flush:(中文翻譯:同花)5張牌同一花色。例如:6H 9H TH JH QH
  7. Full House:(中文翻譯:葫蘆)3張同一點數,另2張同一點數。例如:8H 8D 8C 7S 7D
  8. Four of a kind:(中文翻譯:4條或鐵枝)4張牌同一個點數。例如:8H 8D 8C 8S 9D
  9. Straight flush: (中文翻譯:同花順)5張牌同一花色,並且形成一個順子。例如:8H 9H TH JH QH

給你25張牌排成5*5的樣子。你的任務就是算出5列(橫的)及5欄(直的)這10手牌所形成的統計結果。

luckycat 翻譯

依序判斷,只能硬暴。

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
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
int getSuit(char c) {
switch(c) {
case 'C': return 0;
case 'D': return 1;
case 'H': return 2;
case 'S': return 3;
}
}
int getRank(char c) {
switch(c) {
case '0' ... '9': return c - '0';
case 'A': return 1;
case 'X': return 10;
case 'J': return 11;
case 'Q': return 12;
case 'K': return 13;
}
}
bool suitCmp(pair<int, int> x, pair<int, int> y) {
if(x.first != y.first)
return x.first < y.first;
return x.second < y.second;
}
bool rankCmp(pair<int, int> x, pair<int, int> y) {
if(x.second != y.second)
return x.second < y.second;
return x.first < y.first;
}
int getType(vector< pair<int, int> > cards) {
vector< pair<int, int> >::iterator it;
sort(cards.begin(), cards.end(), suitCmp);
if(cards[0].first == cards[4].first) { // all same suit
for(int i = 0; i < 13; i++) {
int ok = 1;
for(int j = 0; j < 5; j++) {
int r = (i + j)%13 + 1;
it = find(cards.begin(), cards.end(), make_pair(cards[0].first, r));
ok &= it != cards.end();
}
if(ok)
return 9;
}
}
sort(cards.begin(), cards.end(), rankCmp);
if(cards[0].second == cards[3].second ||
cards[1].second == cards[4].second)
return 8;
sort(cards.begin(), cards.end(), rankCmp);
if(cards[0].second == cards[2].second &&
cards[3].second == cards[4].second)
return 7;
if(cards[0].second == cards[1].second &&
cards[2].second == cards[4].second)
return 7;
sort(cards.begin(), cards.end(), suitCmp);
if(cards[0].first == cards[4].first)
return 6;
sort(cards.begin(), cards.end(), suitCmp);
for(int i = 0; i < 13; i++) {
int ok = 1;
for(int j = 0; j < 5; j++) {
int r = (i + j)%13 + 1;
for(int k = 0; k < 4; k++) {
it = find(cards.begin(), cards.end(), make_pair(k, r));
if(it != cards.end())
break;
}
ok &= it != cards.end();
}
if(ok)
return 5;
}
sort(cards.begin(), cards.end(), rankCmp);
if(cards[0].second == cards[2].second ||
cards[1].second == cards[3].second ||
cards[2].second == cards[4].second)
return 4;
sort(cards.begin(), cards.end(), rankCmp);
if(cards[0].second == cards[1].second &&
cards[2].second == cards[3].second)
return 3;
if(cards[0].second == cards[1].second &&
cards[3].second == cards[4].second)
return 3;
if(cards[1].second == cards[2].second &&
cards[3].second == cards[4].second)
return 3;
sort(cards.begin(), cards.end(), rankCmp);
for(int i = 1; i < 5; i++)
if(cards[i].second == cards[i-1].second)
return 2;
return 1;
}
int main() {
int testcase;
char grid[5][5][5];
scanf("%d", &testcase);
while(testcase--) {
for(int i = 0; i < 5; i++)
for(int j = 0; j < 5; j++)
scanf("%s", grid[i][j]);
int cnt[10] = {};
for(int i = 0; i < 5; i++) {
vector< pair<int, int> > cc;
for(int j = 0; j < 5; j++)
cc.push_back(make_pair(getSuit(grid[i][j][1]), getRank(grid[i][j][0])));
int f = getType(cc);
cnt[f]++;
}
for(int i = 0; i < 5; i++) {
vector< pair<int, int> > cc;
for(int j = 0; j < 5; j++)
cc.push_back(make_pair(getSuit(grid[j][i][1]), getRank(grid[j][i][0])));
int f = getType(cc);
cnt[f]++;
}
for(int i = 1; i <= 9; i++) {
if(i > 1) printf(", ");
printf("%d", cnt[i]);
}
puts("");
if(testcase)
puts("");
}
return 0;
}
/*
2
AS 2S 3S 4S 5S
AC 2H 3H 5C 4C
AH 2D KC KH 5D
AD 3D KD 9D 8D
XH 3C XC XS 8C
AS 2S 3S 4S 6S
AC 2H 3H 5C 4C
AH 2D KC KH 5D
AD 3D KD 9D 8D
XH 3C XC XS 8C
*/
Read More +

UVa 12697 - Minimal Subarray Length

Problem

You are given an integer sequence of length N and another value X. You have to find a contiguous subsequence of the given sequence such that the sum is greater or equal to
X. And you have to find that segment with minimal length.

Sample Input

1
2
3
4
5
6
7
3
5 4
1 2 1 2 1
6 -2
-5 -6 -7 -8 -9 -10
5 3
-1 1 1 1 -1

Sample Output

1
2
3
3
-1
3

Solution

題目描述:

找一段長度最小,且該區間總和大於等於指定 X。

題目解法:

離散化後,利用掃描線的思路進行維護。

利用前綴和 SUM[i],查找小於等於 SUM[i] - X 的最大值。

隨後更新 SUM[i] 位置的值 i。

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 <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
using namespace std;
#define MAXN 5024288
#define INF 0x3f3f3f3f
long long A[MAXN], SM[MAXN], SB[MAXN];
// binary index tree
int BIT[MAXN<<1];
void modify(int x, int v, int L) {
while(x <= L) {
BIT[x] = max(BIT[x], v);
x += x&(-x);
}
}
int query(int x) {
int ret = - INF;
while(x) {
ret = max(ret, BIT[x]);
x -= x&(-x);
}
return ret;
}
int main() {
int testcase, N, X;
scanf("%d", &testcase);
while(testcase--) {
scanf("%d %d", &N, &X);
for(int i = 1; i <= N; i++)
scanf("%lld", A+i);
for(int i = 1; i <= N; i++)
SM[i] = SM[i-1] + A[i];
for(int i = 0; i <= N; i++)
SB[i] = SM[i];
for(int i = 1; i <= N + 1; i++)
BIT[i] = - INF;
sort(SB, SB + N + 1);
int M = unique(SB, SB + N + 1) - SB;
int ret = INF;
for(int i = 0; i <= N; i++) {
long long v = SM[i] - X;
int pos = upper_bound(SB, SB + M, v) - SB;
if(pos - 1 >= 0 && i) {
ret = min(ret, i - query(pos));
// printf("query %d\n", i - query(pos));
}
pos = upper_bound(SB, SB + M, SM[i]) - SB;
modify(pos, i, M);
// printf("%lld %d\n", v, pos);
}
printf("%d\n", ret == INF ? -1 : ret);
}
return 0;
}
/*
3
5 4
1 2 1 2 1
6 -2
-5 -6 -7 -8 -9 -10
5 3
-1 1 1 1 -1
*/
Read More +

UVa 428 - Swamp County Roofs

Problem

The Swamp County Environmental Office is concerned about the number of structures in the county. They have a theory that all the rain water landing on roofs of buildings, parking lots, roads, and highways is going down the sewage system instead of draining to the ecologically sensitive and historically important swamp land the county is famous for.

Your team has been hired to do the analysis of rain water captured by building roofs. The Planning Commission, which processes all building permits, and hence knows the plans of every structure in the county, will provide the data. Their database contains a set of measurements for the roofs of every building on every lot in the county.

The Engineering staff of the Planning Commission has determined that all the roofs in the county can be represented by one or more quadrilaterals. Each quadrilateral has the following characteristics:

length of baseline, in feet;
length of ridgeline, in feet;
distance between baseline and ridgeline, in feet;
and inclination of the roof segment, in degrees. 

The baseline is always parallel to the ridgeline. A roof segment is always flat, but may be inclined. The inclination of the segment occurs along the baseline or ridgeline axis. A roof segment that is inclined 90 degrees is a wall that is assumed to have negligible thickness, and hence gathers no rain–such a segment will not appear in the data. Assume that the buildings are on level land, and that no roof segment overlaps any other. A roof segment may have either a baseline or ridgeline length of zero (such a segment is actually a triangle). A lot may contain one or more buildings, but your program need only compute the area covered by roofs on a lot-by-lot basis.

Input

Each lot is represented by a series of numbers separated from each other by white space and/or new-lines. The lot size, in square feet, is followed by a list of roof segments. Each roof segment will consist of the length of the baseline, the length of the ridgeline, the distance between the baseline and the ridgeline, and the inclination. Each lot description including the last will end with a blank line.

Output

For each lot compute and print the following statistics: the total roof surface area of buildings in square feet, the total floor space covered by roofs in square feet, and the percentage of rain intercepted by roofs. Print these values in labeled columns, one lot per line, with two digits after the decimal point. Print percent signs after the percentage of rain figures.

At the end of the run compute and print the following statistics: the total roof surface area of all roofs in the county, the total floor space covered by roofs in the county, the overall percentage of rain intercepted by roofs, the average roof surface area on a lot, and the average floor space covered by roofs on a lot. Skip a line after the per-lot listing and print these values on separate lines with appropriate labels.

Print each value with two digits after the decimal point. Print a percent sign after the overall percentage of rain value. Use a report format similar to that of the sample output below.

One of the reasons Swamp County has remained so is the relative lack of wind in the county–you may therefore assume that all rain falls vertically.

Sample Input

1
2
3
4
100 10 10 10 60
10 10 10 60
200 5 10.5 5 30 10 10 7.5 45

Sample Output

1
2
3
4
5
6
7
8
9
10
Roof Area Floor Area % Covered
--------- ---------- ---------
200.00 100.00 100.00%
113.75 86.59 43.30%
Total surface area of roofs 313.75
Total area covered by roofs 186.59
Percentage of total area covered by roofs 62.20%
Average roof surface area per lot 156.88
Average floor space covered per lot 93.30

Solution

題目描述:

屋頂為一個三維空間的平面,假想成梯形也可以,頃角就是單純地將梯形與平面 x-y 夾角 theta。

分別算出梯形面積和陰影面積。

題目解法:

最坑的就是輸入,其實看了很久都沒有明白到底是怎麼區隔每一組。
查閱了討論區給的測資才湊出 AC 代碼。

想辦法讀到每一組測資是 4n + 1 個數字,否則繼續將空白行讀入。

brianfry713’s Input:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
100 10 10 10 60
10 10 10 60
200 5 10.5 5 30 10 10 7.5 45
100
10 10 10
60 10
10 10 60
200 5 10.5 5 30 10 10 7.5 45
100 0 10 7.5 45 10
0 7.5 -135
300

brianfry713’s Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Roof Area Floor Area % Covered
--------- ---------- ---------
200.00 100.00 100.00%
113.75 86.59 43.30%
0.00 0.00 0.00%
600.00 590.88 5908.85%
7701.25 7133.96 71339.57%
0.00 0.00 0.00%
Total surface area of roofs 8615.00
Total area covered by roofs 7911.43
Percentage of total area covered by roofs 1098.81%
Average roof surface area per lot 1435.83
Average floor space covered per lot 1318.57

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
#include <stdio.h>
#include <sstream>
#include <iostream>
#include <math.h>
using namespace std;
const double pi = acos(-1);
double A, B, C, N;
char* getLot() {
char line[1024], *v;
string ret = "", token;
int tokenCnt = 0;
while(tokenCnt%4 != 1) {
while((v = gets(line)) && line[0] != '\0') {
ret += " ", ret += line;
stringstream ss(line);
while(ss >> token)
tokenCnt++;
}
if(!v)
return v;
}
stringstream sin(ret);
double lotsize, baseline, ridgeline, dist, theta;
double a = 0, b = 0;
sin >> lotsize;
while(sin >> baseline >> ridgeline >> dist >> theta) {
a += (baseline + ridgeline) * dist/2.0;
b += (baseline + ridgeline) * dist/2.0 * cos(theta / 180.0 * pi);
}
printf("%9.2lf %10.2lf %8.2lf%%\n", a, b, b * 100 /lotsize);
A += a, B += b, C += lotsize, N++;
return v;
}
int main() {
// freopen("in.txt", "r+t", stdin);
// freopen("out.txt", "w+t", stdout);
puts("Roof Area Floor Area % Covered");
puts("--------- ---------- ---------");
char line[1024];
while(true) {
if(getLot()) {
} else {
break;
}
}
puts("");
printf("Total surface area of roofs %10.2lf\n", A);
printf("Total area covered by roofs %10.2lf\n", B);
printf("Percentage of total area covered by roofs %6.2lf%%\n", B*100/C);
printf("Average roof surface area per lot %10.2lf\n", A/N);
printf("Average floor space covered per lot %10.2lf\n", B/N);
return 0;
}
Read More +

UVa 11855 - Buzzwords

Problem

The word the is the most common three-letter word. It even shows up inside other words, such as “other” and “mathematics”. Sometimes it hides, split between two words, such as “not here”. Have you ever wondered what the most common words of lengths other than three are?

Your task is the following. You will be given a text. In this text, find the most common word of length one. If there are multiple such words, any one will do. Then count how many times this most common word appears in the text. If it appears more than once, output how many times it appears. Then repeat the process with words of length 2, 3, and so on, until you reach such a length that there is no longer any repeated word of that length in the text.

Input Specification

The input consists of a sequence of lines. The last line of input is empty and should not be processed. Each line of input other than the last contains at least one but no more than one thousand uppercase letters and spaces. The spaces are irrelevant and should be ignored.

Sample Input

1
2
THE OTHER MATHEMATICS NOT HERE
AA

Note that the last line of the sample input is a blank line.

Output Specification

For each line of input, output a sequence of lines, giving the number of repetitions of words of length 1, 2, 3, and so on. When you reach a length such that there are no repeated words of that length, output one blank line, do not output anything further for that input line, and move on to the next line of input.

Output for Sample Input

1
2
3
4
5
6
7
5
4
4
2
2
2

Solution

題目描述:

依序將長度為 1, 2, 3, 4 … 其重複出現次數最高的字串次數輸出,只考慮重複次數大於 1 的所有情況。

題目解法:

由於題目只查看大寫字母,忽略空白不計,若存在有空白的行時特別處理,否則在隨後的 trim() 會有輸出錯誤。

直接套上 Suffix Array,使用 Double algorithm 建造法,並且建出高度數組。

建造在 O(n log n),但是輸出處理最慘會到 O(n^2)

在其他的做法中,可以看到有人使用 hash function 進行比較 (將每一組單詞丟到同一個欄中,之後在進行比較),這也是個挺不錯的做法,但是風險還是存在。

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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct SuffixArray {
int sa[1005], h[1005], n;
int w[1005], ta[1005], tb[1005];
char str[1005];
void sort(int *x, int *y, int m) {
static int i;
for(i = 0; i < m; i++)
w[i] = 0;
for(i = 0; i < n; i++)
w[x[y[i]]]++;
for(i = 1; i < m; i++)
w[i] += w[i-1];
for(i = n-1; i >= 0; i--)
sa[--w[x[y[i]]]] = y[i];
}
bool cmp(int *r, int a, int b, int l) {
if(r[a] == r[b]) {
if(a+l >= n || b+l >= n)
return false;
return r[a+l] == r[b+l];
}
return false;
}
void build_h() {
int i, j, k;
for(i = 0; i < n; i++) ta[sa[i]] = i;
for(i = 0; i < n; i++) {
if(ta[i] == 0) {
h[ta[i]] = 0;
continue;
}
if(i == 0 || h[ta[i-1]] <= 1)
k = 0;
else
k = h[ta[i-1]]-1;
while(str[sa[ta[i]-1]+k] == str[sa[ta[i]]+k])
k++;
h[ta[i]] = k;
}
}
void build() {// x: rank, y: second key
int i, k, m = 128, p;
int *x = ta, *y = tb, *z;
n = strlen(str);
x[n] = 0;
for(i = 0; i < n; i++)
x[i] = str[i], y[i] = i;
sort(x, y, m);
for(k = 1, p = 1; p < n; k *= 2, m = p) {
for(p = 0, i = n-k; i < n; i++)
y[p++] = i;
for(i = 0; i < n; i++) {
if(sa[i] >= k) {
y[p++] = sa[i]-k;
}
}
sort(x, y, m);
z = x, x = y, y = z;
for(i = 1, p = 1, x[sa[0]] = 0; i < n; i++)
x[sa[i]] = cmp(y, sa[i-1], sa[i], k) ? p-1 : p++;
}
}
};
int main() {
SuffixArray in;
while(gets(in.str) && in.str[0] != '\0') {
int n = 0;
for(int i = 0; in.str[i]; i++)
if(in.str[i] != ' ')
in.str[n++] = in.str[i];
in.str[n] = '\0';
in.build();
in.build_h();
if(n == 0)
puts("0");
for(int i = 1; i <= in.n; i++) {
int cnt = 0, ret = 0;
for(int j = 0; j < in.n; j++) {
if(in.h[j] >= i)
cnt++;
else
ret = max(ret, cnt), cnt = 0;
}
ret = max(ret, cnt);
if(ret <= 0)
break;
printf("%d\n", ret + 1);
}
puts("");
}
return 0;
}
Read More +

UVa 349 - Transferable Voting (II)

Problem

The Transferable Vote system for determining the winner of an election requires that a successful candidate obtain an absolute majority of the votes cast, even when there are more than two candidates. To achieve this goal, each voter completes his or her ballot by listing all the candidates in order of preference. Thus if there are five candidates for a single position, each voter’s ballot must contain five choices, from first choice to fifth choice.

In this problem you are to determine the winner, if any, of elections using the Transferable Vote system. If there are c candidates for the single position, then each voter’s ballot will include c distinct choices, corresponding to identifying the voter’s first place, second place, tex2html_wrap_inline32 , and nth place selections. Invalid ballots will be discarded, but their presence will be noted. A ballot is invalid if a voter’s choices are not distinct (choosing the same candidate as first and second choice isn’t permitted) or if any of the choices aren’t legal (that is, in the range 1 to c).

For each election candidates will be assigned sequential numbers starting with 1. The maximum number of voters in this problem will be 100, and the maximum number of candidates will be 5.

            Table A                 Table B
-------------------------------    --------- 
Voter    First   Second  Third
         Choice  Choice  Choice
  1        1       2       4
  2        1       3       2       1   3   2
  3        3       2       1       3   2   1
  4        3       2       1       3   2   1
  5        1       2       3       1   2   3
  6        2       3       1       3   1
  7        3       2       1       3   2   1
  8        3       1       1
  9        3       2       1       3   2   1
 10        1       2       3       1   2   3
 11        1       3       2       1   3   2
 12        2       3       1       3   1

Consider a very small election. In Table A are the votes from the 12 voters for the three candidates. Voters 1 and 8 cast invalid ballots; they will not be counted. This leaves 10 valid ballots, so a winning candidate will require at least 6 votes (the least integer value greater than half the number of valid ballots) to win. Candidates 1 and 3 each have 4 first choice votes, and candidate 2 has two. There is no majority, so the candidate with the fewest first choice votes, candidate 2, is eliminated. If there had been several candidates with the fewest first choice votes, any of them, selected at random, could be selected for elimination.

With candidate 2 eliminated, we advance the second and third choice candidates from those voters who voted for candidate 2 as their first choice. The result of this action is shown in Table B. Now candidate 3 has picked up 2 additional votes, giving a total of 6. This is sufficient for election. Note that if voter 12 had cast the ballot 2 1 3 then there would have been a tie between candidates 1 and 3.

Input

There will be one or more elections to consider. Each will begin with a line containing the number of candidates and the number of voters, c and n. Data for the last election will be followed by a line containing two zeroes.

Following the first line for each election will be n additional lines each containing the choices from a single ballot. Each line will contain only c non-negative integers separated by whitespace.

Output

For each election, print a line identifying the election number (they are numbered sequentially starting with 1). If there were any invalid ballots, print an additional line specifying the number of such. Finally, print a line indicating the winner of the election, if any, or indication of a tie; be certain to identify the candidates who are tied. Look at the samples below for the exact output format.

Sample Input

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
3 12
1 2 4
1 3 2
3 2 1
3 2 1
1 2 3
2 3 1
3 2 1
3 1 1
3 2 1
1 2 3
1 3 2
2 3 1
3 12
1 2 4
1 3 2
3 2 1
3 2 1
1 2 3
2 3 1
3 2 1
3 1 1
3 2 1
1 2 3
1 3 2
2 1 3
4 15
4 3 1 2
4 1 2 3
3 1 4 2
1 3 2 4
4 1 2 3
3 4 2 1
2 4 3 1
3 2 1 4
3 1 4 2
1 4 2 3
3 4 1 2
3 2 1 4
4 1 3 2
3 2 1 4
4 2 1 4
0 0

Sample Output

1
2
3
4
5
6
7
8
9
Election #1
2 bad ballot(s)
Candidate 3 is elected.
Election #2
2 bad ballot(s)
The following candidates are tied: 1 3
Election #3
1 bad ballot(s)
Candidate 3 is elected.

Solution

題目描述:

投票時紀錄自願序,接著每一輪刷掉得票數量最小的候選人,直到所有候選人得票數皆相同或者只剩下一個候選人。若自願序中前位者遭到刷掉,則由下一個志願候選人替補。

如果發現志願序中有重複候選人、或者是不存在的候選人則將此票視為廢票。

題目解法:

模擬!

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
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main() {
int n, m, cases = 0, x;
while(scanf("%d %d", &n, &m) == 2 && n+m) {
int bad[1024] = {}, A[1024][16];
int badCount = 0;
for(int i = 0; i < m; i++) {
int mark[16] = {};
for(int j = 0; j < n; j++) {
scanf("%d", &x);
A[i][j] = x;
if(x > n || x < 1) {
bad[i] = 1;
continue;
}
mark[x]++;
if(mark[x] > 1)
bad[i] = 1;
}
badCount += bad[i];
}
printf("Election #%d\n", ++cases);
printf(" %d bad ballot(s)\n", badCount);
int Atop[1024] = {}, alive[1024] = {}, aliveCount = n;
for(int i = 1; i < n; i++) {
int votes[16] = {}, mx = 0, del = -1;
for(int j = 0; j < m; j++) {
if(bad[j]) continue;
while(alive[A[j][Atop[j]]])
Atop[j]++;
votes[A[j][Atop[j]]]++;
}
for(int j = 1; j <= n; j++)
mx = max(mx, votes[j]);
for(int j = 1; j <= n; j++) {
if(votes[j] < mx && alive[j] == 0)
del = j, mx = votes[j];
}
if(del == -1) break;
alive[del] = 1, aliveCount--;
}
if(aliveCount == 1) {
for(int i = 1; i <= n; i++)
if(alive[i] == 0)
printf(" Candidate %d is elected.\n", i);
} else {
printf(" The following candidates are tied:");
for(int i = 1; i <= n; i++)
if(alive[i] == 0)
printf(" %d", i);
puts("");
}
}
return 0;
}
Read More +

UVa 302 - John's trip

Problem

Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his friends, but there was many of them. In each street he had one friend. He started thinking how to make his trip as short as possible. Very soon he realized that the best way to do it was to travel through each street of town only once. Naturally, he wanted to finish his trip at the same place he started, at his parents’ house.

The streets in Johnny’s town were named by integer numbers from 1 to n, n < 1995. The junctions were independently named by integer numbers from 1 to m, tex2html_wrap_inline32 . All junctions in the town had different numbers. Each street was connecting exactly two (not necessarily different) junctions. No two streets in the town had the same number. He immediately started to plan his round trip. If there was more than one such round trip, he would have chosen the one which, when written down as a sequence of street numbers is lexicographically the smallest.

But Johnny was not able to find even one such round trip. Help Johnny and write a program which finds the desired shortest round trip. If the round trip does not exist the program should write a message. Assume that Johnny lives at the junction ending the 1st street input with smaller number. All streets in the town are two way. There exists a way from each street to another street in the town. The streets in the town are very narrow and there is no possibility to turn back the car once he is in the street.

Input

Input file consists of several blocks. Each block describes one town. Each line in the block contains three integers x, y, z, where x > 0 and y > 0 are the numbers of junctions which are connected by the street number z. The end of the block is marked by the line containing x = y = 0. At the end of the input file there is an empty block, x = y = 0.

Output

The output file consists of 2 line blocks corresponding to the blocks of the input file. The first line of each block contains the sequence of street numbers (single members of the sequence are separated by space) describing Johnny’s round trip. If the round trip cannot be found the corresponding output block contains the message ``Round trip does not exist.’’. The second line of each block is empty.

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
13
1 2 1
2 3 2
3 1 6
1 2 5
2 3 3
3 1 4
0 0
1 2 1
2 3 2
1 3 3
2 4 4
0 0
0 0

Sample Output

1
2
3
1 2 3 5 4 6
Round trip does not exist.

Solution

題目描述:

每一條路上都有編號,並且每個編號唯一且個數小於 44。

求一條經過所有邊且不重複的環道。

題目解法:

記得注意起始是第一條邊最小編號的點。

之後對於每一個點相鄰邊根據編號進行排序即可,按著窮舉思路由小開始枚舉即可。

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
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
struct edge {
int to, label;
edge(int a=0, int b=0):
to(a), label(b) {}
bool operator<(const edge &x) const {
return label < x.label;
}
};
#define MAXN 2048
vector<edge> g[MAXN];
int label_used[MAXN];
deque<int> path;
void dfs(int u) {
for(int i = 0; i < g[u].size(); i++) {
if(label_used[g[u][i].label] == 0) {
label_used[g[u][i].label] = 1;
dfs(g[u][i].to);
path.push_front(g[u][i].label);
}
}
}
int main() {
int x, y, z;
while(true) {
for(int i = 0; i < MAXN; i++)
g[i].clear();
int e = 0, start = 0;
while(scanf("%d %d", &x, &y) == 2) {
if(x == 0 && y == 0)
break;
/* Johnny lives at the junction ending the 1st street
input with smaller number. */
if(e == 0)
start = min(x, y);
scanf("%d", &z);
g[x].push_back(edge(y, z));
g[y].push_back(edge(x, z));
e++;
}
if(e == 0) break;
int odd = 1;
for(int i = 0; i < MAXN; i++) {
odd &= g[i].size()%2 == 0;
}
if(!odd) {
puts("Round trip does not exist.\n");
continue;
}
for(int i = 0; i < MAXN; i++)
sort(g[i].begin(), g[i].end());
memset(label_used, 0, sizeof(label_used));
path.clear();
dfs(start);
for(int i = 0; i < path.size(); i++)
printf("%d%c", path[i], i == path.size()-1 ? '\n' : ' ');
puts("");
}
return 0;
}
Read More +

UVa 11837 - Musical Plagiarism

Problem

The musical notes are the basic units of Western music. Each note is associated with a frequency. Two notes for which the fundamental frequencies have a ratio of power of 2 (one is half of the other, one is double of the other, etc..) are perceived as very similar. Therefore, any notes with that kind of relationship are given the same name, as described below.

There are twelve basic notes in a sequence of increasing frequency, each note separated from the previous note by the same distance in the musical scale (this distance is called a semitone). Seven of these twelve notes are represented by letters of the alphabet (A, B, C, D, E, F and G). The table below shows the distance, in semitones, between the notes.

Notes A-B B-C C-D D-E E-F F-G G-A
Number of semitone 2 1 2 2 1 2 2

Notice that there are five notes that are not represented by letters of the alphabet: the notes between A and B, between C and D, between D and E, betwen F and G and between G and A.

Notes can be modified by two accidentals, called sharp and flat, represented respectively by the symbols #' andb’. A sharp raises the note a semitone, a flat lowers the note a semitone. A note with an accidental is denoted by the name of the note followed by the accidental symbol. Notice that with this scheme we can represent all twelve notes.

The figure below illustrates the name of the notes in accordance with the scheme described above, in a fragment of a piano keyboard.

A melody can be represented by a sequence of notes. For example,

A A D C# C# D E E E F# A D G# A

is a well known melody. Note however that as the distances between the semitones are always equal, the same melody can be written starting with another note (we say that the melody is in another key):

B B E D# D# E Gb Gb Gb G# B E A# B

Your neighbor is a famous composer who suspects someone has plagiarized one of her songs. She asked your help to write a program that, given the sequence of notes of the melody in her song, and the sequence of notes of a suspicious snippet of melody, determines if the suspicious snippet occurs, in some key, in her song.

Input

The input consists of several test cases. The first line of a test case contains two integers M and T ( 1$\le$M$\le$105, 1$\le$T$\le$104, T$\le$M), indicating the number of notes in the song suspected of having been plagiarized and in the suspect snippet. Each of the next two lines contains M and T notes, respectively, indicating the notes of the song and of the suspect snippet.

Notes in each line are separated by one space; each note is one among A',B’, C',D’, E',F’ or G', possibly followed by an accidental sign:#’ for sharp or `b’ for flat.

The last test case is followed by a line containing only two zeros separated by a blank space.

Output

For each test case your program must print a single line containing one character, S' in case the song has been plagiarized by the text, orN’ in case the song has not been plagiarized by the text.

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
13
16 4
D G A B C D G G G C D E F# G C C
G G C D
12 2
C C# D D# E F F# G G# A A# B
C D
12 2
C Db D Eb E F Gb G Ab A Bb B
C D
4 3
C E G Bb
D F# A
0 0

Sample Output

1
2
3
4
S
N
N
S

Solution

題目描述:

音階 全全半全全全半,全音中間夾一個黑/白鍵,半音之間沒有。

計算是否為旋律中的一部分,計算旋律時只需考慮音符之間的距離即可,與高低幾度偏移沒有關係。

題目解法:

特別小心,查詢的旋律長度為 1 的時候,無法判斷 (雖然以子字串是有在的)。

接著,直接對距離之間取模之後得到兩個整數序列,直接套用 KMP 判斷序列是否在另一個序列中。

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
#include <stdio.h>
#include <algorithm>
using namespace std;
int notes[128] = {};
int kmpTable[500005];
void KMPtable(int *P, int len) {
int i, j;
kmpTable[0] = -1, i = 1, j = -1;
while(i < len) {
while(j >= 0 && P[j+1] != P[i])
j = kmpTable[j];
if(P[j+1] == P[i])
j++;
kmpTable[i++] = j;
}
}
int KMPmatching(int T[], int P[], int tlen, int plen) {
for(int i = 0, j = -1; i < tlen; i++) {
while(j >= 0 && P[j+1] != T[i])
j = kmpTable[j];
if(P[j+1] == T[i]) j++;
if(j == plen-1) {
j = kmpTable[j];
return i;
}
}
return -1;
}
int main() {
int M, T;
int A[100005], B[10005];
char s[10];
notes['C'] = 0, notes['D'] = 2;
notes['E'] = 4, notes['F'] = 5;
notes['G'] = 7, notes['A'] = 9;
notes['B'] = 11;
while(scanf("%d %d", &M, &T) == 2 && M + T) {
int prev, x;
for(int i = 0; i < M; i++) {
scanf("%s", &s);
x = (notes[s[0]] + (s[1] == '#') - (s[1] == 'b') + 12)%12;
if(i) A[i - 1] = (prev - x + 12)%12;
prev = x;
}
for(int i = 0; i < T; i++) {
scanf("%s", &s);
x = (notes[s[0]] + (s[1] == '#') - (s[1] == 'b') + 12)%12;
if(i) B[i - 1] = (prev - x + 12)%12;
prev = x;
}
KMPtable(A, M - 1);
puts( KMPmatching(A, B, M - 1, T - 1) >= 0 ? "S" : "N");
}
return 0;
}
Read More +