Cycles of Lanes

Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1062 Accepted: 392

Description
Each of the M lanes of the Park of Polytechnic University of Bucharest connects two of the N crossroads of the park (labeled from 1 to N). There is no pair of crossroads connected by more than one lane and it is possible to pass from each crossroad to each other crossroad by a path composed of one or more lanes. A cycle of lanes is simple when passes through each of its crossroads exactly once.
The administration of the University would like to put on the lanes pictures of the winners of Regional Collegiate Programming Contest in such way that the pictures of winners from the same university to be on the lanes of a same simple cycle. That is why the administration would like to assign the longest simple cycles of lanes to most successful universities. The problem is to find the longest cycles? Fortunately, it happens that each lane of the park is participating in no more than one simple cycle (see the Figure).

Input

On the first line of the input file the number T of the test cases will be given. Each test case starts with a line with the positive integers N and M, separated by interval (4 <= N <= 4444). Each of the next M lines of the test case contains the labels of one of the pairs of crossroads connected by a lane.

Output

For each of the test cases, on a single line of the output, print the length of a maximal simple cycle.

Sample Input

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

Sample Output

4

Source

Southeastern European Regional Programming Contest 2009

题意 求无向图中的最长环

直接dfs 依次对访问每个点编号 遇到已经访问过的点就是环了 编号相减就是环的长度

用数组存图会超内存 所以要用vector存

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
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 4500;
vector<int> ma[N];
int vis[N], a, b, cas, n, m, ans;
int dfs (int i, int no)
{
vis[i] = no;
for (int j = 0; j < ma[i].size(); ++j)
{
if (!vis[ma[i][j]]) dfs (ma[i][j], no + 1);
else ans = max (ans, no - vis[ma[i][j]] + 1);
}
}

int main()
{
scanf ("%d", &cas);
while (cas--)
{
scanf ("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) ma[i].clear();
for (int i = 1; i <= m; ++i)
{
scanf ("%d%d", &a, &b);
ma[a].push_back (b);
ma[b].push_back (a);
}
ans = 0;
memset (vis, 0, sizeof (vis));
for (int i = 1; i <= n; ++i)
if (!vis[i]) dfs (i, 1);
if (ans >= 3) printf ("%d\n", ans);
else printf ("0\n");
}
return 0;
}

Print Words in Lines

Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1624 Accepted: 864

Description
We have a paragraph of text to print. A text is a sequence of words and each word consists of characters. When we print a text, we print the words from the text one at a time, according to the order the words appear in the text. The words are printed in lines, and we can print at most M characters in a line. If there is space available, we could print more than one word in a line. However, when we print more than one word in a line, we need to place exactly one space character between two adjacent words in a line. For example, when we are given a text like the following:
This is a text of fourteen words and the longest word has ten characters

Now we can print this text into lines of no more than 20 characters as the following.

This is
a text of
fourteen words
and the longest
word
has ten characters

However, when you print less than 20 characters in a line, you need to pay a penalty, which is equal to the square of the difference between 20 and the actual number of characters you printed in that line. For example in the first line we actually printed seven characters so the penalty is (20 − 7)2 = 169. The total penalty is the sum of all penalties from all lines. Given the text and the number of maximum number of characters allowed in a line, compute the minimum penalty to print the text.

Input

The first line of the input is the number of test cases (C). The first line of a test case is the maximum number of characters allowed in a line (M). The second line of a test case is the number of words in the text (N). The following N lines are the length (in character) of each word in the text. It is guaranteed that no word will have more than M characters, N is at most 10000, and M is at most 100.

Output

The output has C lines. Each line has the minimum penalty one needs to pay in order to print the text in that test case.

Sample Input

2 20 14 4 2 1 4 2 8 5 3 3 7 4 3 3 10 30 14 4 2 1 4 2 8 5 3 3 7 4 3 3 10

Sample Output

33 146

Source

Kaohsiung 2006

题意 把n个单词排版 每行最多m个字符 不同单词间有空格 每行最后一个单词后没空格 空格占一个字符 当一行的字符数与m的差为t时 就会扣t/*t分 求最少扣分

令a[i]表示第i个单词的长度 s[i]表示从第一个单词到第i个单词单词长度和d[i]表示前i个单词排版后最少扣的分

则t=m-(s[i] - s[j] + i - j - 1)表示把从第j+1个单词到第i个单词放在一行时这行的字符长度与m的差

那么当t>=0时 有转移方程 d[i]=min(d[i],d[j]+t/*t) ;

有了方程程序就好写了:

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
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 10005;
int s[N], d[N], a[N], t, cas, m, n;
int main()
{
scanf ("%d", &cas);
while (cas--)
{
scanf ("%d%d", &m, &n);
for (int i = 1; i <= n; ++i)
{
scanf ("%d", &a[i]);
s[i] = s[i - 1] + a[i];
}
memset (d, 0x3f, sizeof (d)); d[0] = 0;
for (int i = 1; i <= n; ++i)
for (int j = i - 1; j >= 0; --j)
{
t = m - (s[i] - s[j] + i - j - 1);
if (t >= 0) d[i] = min (d[i], d[j] + t * t);
else break;
}
printf ("%d\n", d[n]);
}
}


Some message encoding schemes require that an encoded message be sent in two parts. The first part, called the header, contains the characters of the message. The second part contains a pattern that represents the message. You must write a program that can decode messages under such a scheme.

The heart of the encoding scheme for your program is a sequence of ``key” strings of 0’s and 1’s as follows:

displaymath26

The first key in the sequence is of length 1, the next 3 are of length 2, the next 7 of length 3, the next 15 of length 4, etc. If two adjacent keys have the same length, the second can be obtained from the first by adding 1 (base 2). Notice that there are no keys in the sequence that consist only of 1’s.

The keys are mapped to the characters in the header in order. That is, the first key (0) is mapped to the first character in the header, the second key (00) to the second character in the header, the kth key is mapped to the kth character in the header. For example, suppose the header is:

AB/#TANCnrtXc

Then 0 is mapped to A, 00 to B, 01 to /#, 10 to T, 000 to A, …, 110 to X, and 0000 to c.

The encoded message contains only 0’s and 1’s and possibly carriage returns, which are to be ignored. The message is divided into segments. The first 3 digits of a segment give the binary representation of the length of the keys in the segment. For example, if the first 3 digits are 010, then the remainder of the segment consists of keys of length 2 (00, 01, or 10). The end of the segment is a string of 1’s which is the same length as the length of the keys in the segment. So a segment of keys of length 2 is terminated by 11. The entire encoded message is terminated by 000 (which would signify a segment in which the keys have length 0). The message is decoded by translating the keys in the segments one-at-a-time into the header characters to which they have been mapped.

Input

The input file contains several data sets. Each data set consists of a header, which is on a single line by itself, and a message, which may extend over several lines. The length of the header is limited only by the fact that key strings have a maximum length of 7 (111 in binary). If there are multiple copies of a character in a header, then several keys will map to that character. The encoded message contains only 0’s and 1’s, and it is a legitimate encoding according to the described scheme. That is, the message segments begin with the 3-digit length sequence and end with the appropriate sequence of 1’s. The keys in any given segment are all of the same length, and they all correspond to characters in the header. The message is terminated by 000.

Carriage returns may appear anywhere within the message part. They are not to be considered as part of the message.

Output

For each data set, your program must write its decoded message on a separate line. There should not be blank lines between messages.

Sample input

TNM AEIOU 0010101100011 1010001001110110011 11000 $/#//\ 0100000101101100011100101000

Sample output

TAN ME /#/#/*$

题意 编写一个解码程序 对数字串进行解码

输入第一行是一个解码key key从左到右每个字符分别对应0,00,01,10,000,001,011,100,101,110,0000,0001,…,1101,1110,00000,…….

长度为len的字符编码有2^n-1个 而且恰好以二进制方式从0到2^n-2递增 而且字符编码的最大长度为7 可以有2^7-1=127个字符

我们只需开一个key[len][val]数组 里面存的是长度为len的第val+1个字符编码 然后解码时对应输出每个字符就行

如样例2 解码key为”S/#//"

key[1][0]对应编码’0’存的字符为’$’ key[2][0]对应编码’00’存的字符为’/#’

key[2][1]对应编码’01’存的字符为’/‘ key[2][2]对应编码’10’存的字符为’/

key[3][0]对应编码’000’存的字符为’'

需要解码的文本由多个小节组成 每个小节的前三个数字代表小节中每个编码的长度(用二进制表示,例如010表示2) 然后是该长度的不定个字符编码 以全1结束 如长度为2时 ‘11’就表示结束 长度为000时表示需要编码的文本结束;

此题是算法竞赛入门经典第二版中的例题 第83页 里面有详细的讲解;

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
#include<cstdio>
#include<cstring>
#include<cctype>
using namespace std;
const int N = 1000;
char s[N], key[8][1 << 8], c;
int val, len, l;

void read()
{
len = 1; l = strlen (s);
for (int i = val = 0; i < l; ++i)
if (val < ( (1 << len) - 1))
key[len][val++] = s[i];
else
{
val = 0;
key[++len][val++] = s[i];
}
}

void print ()
{
for (int i = val = 0; i < len; ++i)
{
do scanf ("%c", &c); while (!isdigit (c));
val = val * 2 + (c - '0');
}
if (val >= ( (1 << len) - 1)) return;
else
{
printf ("%c", key[len][val]);
print();
}
}

int main()
{
int l1, l2, l3;
while (gets (s) != NULL)
{
if (!s[0]) continue;
memset (key, 0, sizeof (key));
read();
while (scanf ("%1d%1d%1d", &l1, &l2, &l3), len = 4 * l1 + 2 * l2 + l3)
print ();
printf ("\n");
}
return 0;
}


MasterMind is a game for two players. One of them, Designer, selects a secret code. The other,Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players agree upon the length N that a code must have and upon the colors that may occur in a code.

In order to break the code, Breaker makes a number of guesses, each guess itself being a code. After each guess Designer gives a hint, stating to what extent the guess matches his secret code.

In this problem you will be given a secret code tex2html_wrap_inline35 and a guess tex2html_wrap_inline37 , and are to determine the hint. A hint consists of a pair of numbers determined as follows.

A match is a pair (i,j), tex2html_wrap_inline41 and tex2html_wrap_inline43 , such that tex2html_wrap_inline45 . Match (i,j) is called strongwhen i = j, and is called weak otherwise. Two matches (i,j) and (p,q) are called independent wheni = p if and only if j = q. A set of matches is called independent when all of its members are pairwise independent.

Designer chooses an independent set M of matches for which the total number of matches and the number of strong matches are both maximal. The hint then consists of the number of strong followed by the number of weak matches in M. Note that these numbers are uniquely determined by the secret code and the guess. If the hint turns out to be (n,0), then the guess is identical to the secret code.

Input

The input will consist of data for a number of games. The input for each game begins with an integer specifying N (the length of the code). Following these will be the secret code, represented as N integers, which we will limit to the range 1 to 9. There will then follow an arbitrary number of guesses, each also represented as N integers, each in the range 1 to 9. Following the last guess in each game will be N zeroes; these zeroes are not to be considered as a guess.

Following the data for the first game will appear data for the second game (if any) beginning with a new value for N. The last game in the input will be followed by a single zero (when a value forN would normally be specified). The maximum value for N will be 1000.

Output

The output for each game should list the hints that would be generated for each guess, in order, one hint per line. Each hint should be represented as a pair of integers enclosed in parentheses and separated by a comma. The entire list of hints for each game should be prefixed by a heading indicating the game number; games are numbered sequentially starting with 1. Look at the samples below for the exact format.

Sample Input

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

Sample Output

Game 1: (1,1) (2,0) (1,2) (1,2) (4,0) Game 2: (2,4) (3,2) (5,0) (7,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
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
const int N = 1005;
int a[N], b[N], c, d;
int main()
{
int n, cas = 0;
while (scanf ("%d", &n), n)
{
map<int, int> aa;
printf ("Game %d:\n", ++cas);
for (int i = 0; i < n; ++i)
{
scanf ("%d", &a[i]);
++aa[a[i]];
}
while (1)
{
map<int, int> bb;
for (int i = c = d = 0; i < n; ++i)
{
scanf ("%d", &b[i]);
++bb[b[i]];
if (a[i] == b[i]) c++;
}
if (!b[0]) break;
for (int i = 1; i <= 9; ++i)
d += min (aa[i], bb[i]);
printf (" (%d,%d)\n", c, d - c);
}
}
return 0;
}

Ancient Cipher

Ancient Roman empire had a strong government system with various departments, including a secret service department. Important documents were sent between provinces and the capital in encrypted form to prevent eavesdropping. The most popular ciphers in those times were so calledsubstitution cipherandpermutation cipher. Substitution cipher changes all occurrences of each letter to some other letter. Substitutes for all letters must be different. For some letters substitute letter may coincide with the original letter. For example, applying substitution cipher that changes all letters from A' to Y’ to the next ones in the alphabet, and changes Z' to A’, to the message VICTORIOUS'' one gets the message WJDUPSJPVT’’. Permutation cipher applies some permutation to the letters of the message. For example, applying the permutation$ \langle$2, 1, 5, 4, 3, 7, 6, 10, 9, 8$ \rangle$to the message VICTORIOUS'' one gets the message IVOTCIRSUO’’. It was quickly noticed that being applied separately, both substitution cipher and permutation cipher were rather weak. But when being combined, they were strong enough for those times. Thus, the most important messages were first encrypted using substitution cipher, and then the result was encrypted using permutation cipher. Encrypting the message VICTORIOUS'' with the combination of the ciphers described above one gets the message JWPUDJSTVP’’. Archeologists have recently found the message engraved on a stone plate. At the first glance it seemed completely meaningless, so it was suggested that the message was encrypted with some substitution and permutation ciphers. They have conjectured the possible text of the original message that was encrypted, and now they want to check their conjecture. They need a computer program to do it, so you have to write one.

Input file contains several test cases. Each of them consists of two lines. The first line contains the message engraved on the plate. Before encrypting, all spaces and punctuation marks were removed, so the encrypted message contains only capital letters of the English alphabet. The second line contains the original message that is conjectured to be encrypted in the message on the first line. It also contains only capital letters of the English alphabet. The lengths of both lines of the input file are equal and do not exceed 100.

For each test case, print one output line. Output YES ' if the message on the first line of the input file could be the result of encrypting the message on the second line, or NO ‘ in the other case.

JWPUDJSTVP VICTORIOUS MAMA ROME HAHA HEHE AAA AAA NEERCISTHEBEST SECRETMESSAGES

YES NO YES YES NO

题意 给两个字符串a,b 能否把b重排后再把每个字母映射到另一个字母得到a 由于可以重排 出现的顺序就不用考虑了 只要分别把ab中每个字母出现的次数存在数组中 再对数组排序 要是得到的两个数组完全相同b就能转换为a了;

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
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 105;
char a[N], b[N];

int main()
{
while (scanf ("%s%s", a, b) != EOF)
{
int ca[26] = {0}, cb[26] = {0}, l = strlen (a), i;
for (i = 0; i < l; ++i)
{
++ca[a[i] - 'A'];
++cb[b[i] - 'A'];
}
sort (ca, ca + 26);
sort (cb, cb + 26);
for (i = 0; i < 26; ++i)
if (ca[i] != cb[i]) break;
printf (i == 26 ? "YES\n" : "NO\n");
}
return 0;
}


In ``Hangman Judge,’’ you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follows:

Your task as the ``Hangman Judge’’ is to determine, for each game, whether the contestant wins, loses, or fails to finish a game.

Input

Your program will be given a series of inputs regarding the status of a game. All input will be in lower case. The first line of each section will contain a number to indicate which round of the game is being played; the next line will be the solution to the puzzle; the last line is a sequence of the guesses made by the contestant. A round number of -1 would indicate the end of all games (and input).

Output

The output of your program is to indicate which round of the game the contestant is currently playing as well as the result of the game. There are three possible results:

You win. You lose. You chickened out.

Sample Input

1 cheese chese 2 cheese abcdefg 3 cheese abcdefgij -1

Sample Output

Round 1 You win. Round 2 You chickened out. Round 3 You lose.

一个字符串的游戏 有一个你不知道的字符串 你开始有7条命 然后你每次猜一个字母 若你猜的字母在原字符串中 原字符串就去掉所有那个字母 否则你失去一条命 如果你在命耗完之前原字符串中所有的字母都被猜出 则你赢 如果你在命耗完了原字符串中还有字母没被猜出 则你输 如果你在命没耗完原字符串中也还有字母没被猜出 视为你放弃

给你另一个字符串作为你猜的顺序 判断你是否能赢:

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
//UVa489 Hangman
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1000;
char a[N], b[N];
int cas, la, lb, i, j;
int main()
{
while (scanf ("%d", &cas), cas + 1)
{
scanf ("%s %s", a, b);
la = strlen (a);
lb = strlen (b);
int ca[26] = {0}, left = 7;
for (i = 0; i < la; ++i)
++ca[a[i] - 'a'];
for (i = 0; i < lb; ++i)
if (ca[b[i] - 'a'] != 0)
{
ca[b[i] - 'a'] = 0;
for (j = 0; j < 26; ++j)
if (ca[j] != 0) break;
if (j == 26)
{
printf ("Round %d\nYou win.\n", cas);
break;
}
}
else if (--left == 0)
{
printf ("Round %d\nYou lose.\n", cas);
break;
}
if (i == lb) printf ("Round %d\nYou chickened out.\n", cas);
}
return 0;
}


In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is arbitrarily chosen as number 1, and the rest are numbered counter-clockwise up to N (who will be standing on 1’s left). Starting from 1 and moving counter-clockwise, one labour official counts off k applicants, while another official starts from N and moves clockwise, counting m applicants. The two who are chosen are then sent off for retraining; if both officials pick the same person she (he) is sent off to become a politician. Each official then starts counting again at the next available person and the process continues until no-one is left. Note that the two victims (sorry, trainees) leave the ring simultaneously, so it is possible for one official to count a person already selected by the other official.

Input

Write a program that will successively read in (in that order) the three numbers (N, k and m; k, m > 0, 0 < N < 20) and determine the order in which the applicants are sent off for retraining. Each set of three numbers will be on a separate line and the end of data will be signalled by three zeroes (0 0 0).

Output

For each triplet, output a single line of numbers specifying the order in which people are chosen. Each number should be in a field of 3 characters. For pairs of numbers list the person chosen by the counter-clockwise official first. Separate successive pairs (or singletons) by commas (but there should not be a trailing comma).

Sample input

10 4 3 0 0 0

Sample output

tex2html_wrap_inline34 4 tex2html_wrap_inline34 8, tex2html_wrap_inline34 9 tex2html_wrap_inline34 5, tex2html_wrap_inline34 3 tex2html_wrap_inline34 1, tex2html_wrap_inline34 2 tex2html_wrap_inline34 6, tex2html_wrap_inline50 10, tex2html_wrap_inline34 7

where tex2html_wrap_inline50 represents a space.

N个人按逆时针从一到n排成一个环 官员1从1开始每次逆时针走过k个人 选出停留地方的人 官员2从n开始每次顺时针走过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
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 22;
int a[N], n, k, m, r, t1, t2;

int main()
{
while (scanf ("%d%d%d", &n, &k, &m), n)
{
memset (a, -1, sizeof (a));
t2 = 1;
t1 = r = n;
while (r)
{
for (int i = 1; i <= k; ++i)
do t1 = ((t1 == n) ? 1 : t1 + 1); while (a[t1] == 0);
for (int i = 1; i <= m; ++i)
do t2 = ((t2 == 1) ? n : t2 - 1); while (a[t2] == 0);

r = (t1 == t2 ? r - 1 : r - 2);
if (t1 == t2) printf ("%3d", t1);
else printf ("%3d%3d", t1, t2);
a[t1] = a[t2] = 0;
if (r) printf (",");
}
printf ("\n");
}
return 0;
}

题意 你有n/*m个小兵排成一个矩阵 每个在矩阵里的小兵有两种状态 0睡着 或 1清醒 每秒都会发生如下事件 睡着的小兵在他周围恰好有3个醒着的小兵时会醒过来 醒着的 小兵在周围醒着的的小兵数大于3或小于2时会睡着 还有k个小兵会在一定的时间离开 X 求f秒之后所有小兵的状态

直接模拟就行咯~~

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
#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
const int N=55,F=1005;
#define r (x+i)
#define c (y+j)
char s[N][N],cn[N][N];
int n,m;
int cnt(int x,int y)
{
int ccc=0;
for(int i=-1; i<=1; ++i)
for(int j=-1; j<=1; ++j)
if(r>0&&r<=n&&c>0&&c<=m&&(i||j))
{
if(s[r][c]=='1') ++ccc;
}
return ccc;
}

void turn(int x,int y)
{
int cc=cn[x][y];
if(s[x][y]=='0'&&cc==3)
s[x][y]='1';
else if(s[x][y]=='1'&&((cc<2)||(cc>3)))
s[x][y]='0';
}

int main()
{
int cas,f,k,t,x,y,row,col;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d%d%d",&n,&m,&f,&k);
for(int i=1; i<=n; ++i)
scanf("%s",s[i]+1);
stack<int> lx[F],ly[F];
for(int i=1; i<=k; ++i)
{
scanf("%d%d%d",&t,&x,&y);
lx[t].push(x);
ly[t].push(y);
}

for(t=1; t<=f; ++t)
{
for(int i=1; i<=n; ++i)
for(int j=1; j<=m; ++j)
cn[i][j]=cnt(i,j);

for(int i=1; i<=n; ++i)
for(int j=1; j<=m; ++j)
turn(i,j);

while(!(lx[t].empty()))
{
row=lx[t].top();
col=ly[t].top();
s[row][col]='X';
lx[t].pop();
ly[t].pop();
}
}
for(int i=1; i<=n; ++i)
printf("%s\n",s[i]+1);
}
return 0;
}

YY’s Minions Time Limit:2 Seconds Memory Limit:65536 KB

Despite YY’s so much homework, she would like to take some time to play with her minions first.

YY lines her minions up to anN/*Mmatrix. Every minionhas two statuses: awake or asleep. We use 0(the digit) to represent that it is asleep, and 1 for awake. Also, we define the minions who are around a minionclosestin one of theeightdirections its neighbors. And every minute every minion will change its status by the following specific rules:
If this minion is awake, and the number of its neighbors who are awake is less than 2, this minion will feel lonely and turn to asleep.If this minion is awake, and the number of its neighbors who are awake is more than 3, this minion will turn to asleep for it will feel too crowded.If this minion is awake, and the number of its neighbors who are awake is exactly 2 or 3, this minion will keep being awake and feel very happy.If this minion is asleep, and the number of its neighbors who are awake is exactly 3, this minion will wake up because of the noise.

Note that all changes take place at the same time at the beginning of a specific minute.

Also, some minions will get bored and leave this silly game. We use ‘X’s to describe them. We suppose that a minion would leave afterTminutes. It will leave at the end of the Tthminute. Its status is considered during the change at the beginning of the Tthminute, and should be ignored after that. Of course, one minion will not leave twice!

YY is a girl full of curiosity and wants to know every minion’s status afterFminutes. But you know she is weak and lazy! Please help this cute girl to solve this problem :)

Input

There are multiple test cases.

The first line contains the number of test casesQ. 1<=Q<=100.
For each case, there are several lines:
The first line contains four integersN,M,F,K.Kmeans the number of leaving messages. 1<=N,M<=50, 1<=F<=1000, 1<=K<=N/*M.
NextNlines are the matrix which shows the initial status of each minion. Each line containsMchars. We guarantee that ‘X’ wouldn’t appear in initial status matrix.
And nextKlines are the leaving messages. Each line contains three integersTi,Xi,Yi, They mean the minion who is located in (Xi,Yi) will leave the game at the end of theTithminutes. 1<=Ti<=F, 1<=Xi<=N, 1<=Yi<=M.

Output

For each case, outputNlines as a matrix which shows the status of each minion afterFminutes.

Sample Input

2 3 3 2 1 101 110 001 1 2 2 5 5 6 3 10111 01000 00000 01100 10000 2 3 3 2 4 1 5 1 5

Sample Output

010 1X0 010 0000X 11000 00X00 X0000 00000

Hint

For case 1: T=0, the game starts 101 110 001 ————— at the beginning of T=1, a change took place 100 101 010 ————— at the end of T=1 (the minion in (2,2) left) 100 1X1 010 ————— at the beginning of T=2, a change took place 010 1X0 010 ————— at the end of T=2 (nothing changed for no minion left at T=2) 010 1X0 010



题意 猜数字游戏 统计猜的数字有多少个数字位置正确 有多少个数字在答案中出现但是位置不正确 每个字符只能匹配一次

直接匹配每位数

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
#include<cstdio>  
#include<algorithm>
#include<map>
using namespace std;
const int N = 1005;
int a[N], b[N], c, d;
int main()
{
int n, cas = 0;
while (scanf ("%d", &n), n)
{
map<int, int> aa;
printf ("Game %d:\n", ++cas);
for (int i = 0; i < n; ++i)
{
scanf ("%d", &a[i]);
++aa[a[i]];
}
while (1)
{
map<int, int> bb;
for (int i = c = d = 0; i < n; ++i)
{
scanf ("%d", &b[i]);
++bb[b[i]];
if (a[i] == b[i]) c++;
}
if (!b[0]) break;
for (int i = 1; i <= 9; ++i)
d += min (aa[i], bb[i]);
printf (" (%d,%d)\n", c, d - c);
}
}
return 0;
}


MasterMind is a game for two players. One of them, Designer, selects a secret code. The other,Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players agree upon the length N that a code must have and upon the colors that may occur in a code.

In order to break the code, Breaker makes a number of guesses, each guess itself being a code. After each guess Designer gives a hint, stating to what extent the guess matches his secret code.

In this problem you will be given a secret code tex2html_wrap_inline35 and a guess tex2html_wrap_inline37 , and are to determine the hint. A hint consists of a pair of numbers determined as follows.

A match is a pair (i,j), tex2html_wrap_inline41 and tex2html_wrap_inline43 , such that tex2html_wrap_inline45 . Match (i,j) is called strongwhen i = j, and is called weak otherwise. Two matches (i,j) and (p,q) are called independent wheni = p if and only if j = q. A set of matches is called independent when all of its members are pairwise independent.

Designer chooses an independent set M of matches for which the total number of matches and the number of strong matches are both maximal. The hint then consists of the number of strong followed by the number of weak matches in M. Note that these numbers are uniquely determined by the secret code and the guess. If the hint turns out to be (n,0), then the guess is identical to the secret code.

Input

The input will consist of data for a number of games. The input for each game begins with an integer specifying N (the length of the code). Following these will be the secret code, represented as N integers, which we will limit to the range 1 to 9. There will then follow an arbitrary number of guesses, each also represented as N integers, each in the range 1 to 9. Following the last guess in each game will be N zeroes; these zeroes are not to be considered as a guess.

Following the data for the first game will appear data for the second game (if any) beginning with a new value for N. The last game in the input will be followed by a single zero (when a value forN would normally be specified). The maximum value for N will be 1000.

Output

The output for each game should list the hints that would be generated for each guess, in order, one hint per line. Each hint should be represented as a pair of integers enclosed in parentheses and separated by a comma. The entire list of hints for each game should be prefixed by a heading indicating the game number; games are numbered sequentially starting with 1. Look at the samples below for the exact format.

Sample Input

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

Sample Output

Game 1: (1,1) (2,0) (1,2) (1,2) (4,0) Game 2: (2,4) (3,2) (5,0) (7,0)

题意 给你一个环形串 输出它以某一位为起点顺时针得到串的最小字典序

直接模拟 每次后移一位比较字典序即可 注意不能用strcpy(s+1,s)这样后移 strcpy复制地址不能有重叠部分

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
#include<cstdio>  
#include<cstring>
using namespace std;
const int N = 150;
char s[N], ans[N], c;
int t, l;

int main()
{
scanf ("%d", &t);
while (t--)
{
scanf ("%s", s);
l = strlen (s);
strcpy (ans, s);
for (int i = 0; i < l; ++i)
{
c = s[l - 1];
for (int j = l - 1; j >= 1 ; --j)
s[j] = s[j - 1];
s[0] = c;
if (strcmp (s, ans) < 0)
strcpy (ans, s);
}
printf ("%s\n", ans);
}
}

Some DNA sequences exist in circular forms as in the following figure, which shows a circular sequence CGAGTCAGCT", that is, the last symbol T” in CGAGTCAGCT" is connected to the first symbol C”. We always read a circular sequence in the clockwise direction.

\epsfbox{p3225.eps}

Since it is not easy to store a circular sequence in a computer as it is, we decided to store it as a linear sequence. However, there can be many linear sequences that are obtained from a circular sequence by cutting any place of the circular sequence. Hence, we also decided to store the linear sequence that is lexicographically smallest among all linear sequences that can be obtained from a circular sequence.

Your task is to find the lexicographically smallest sequence from a given circular sequence. For the example in the figure, the lexicographically smallest sequence is ``AGCTCGAGTC”. If there are two or more linear sequences that are lexicographically smallest, you are to find any one of them (in fact, they are the same).

The input consists of T test cases. The number of test cases T is given on the first line of the input file. Each test case takes one line containing a circular sequence that is written as an arbitrary linear sequence. Since the circular sequences are DNA sequences, only four symbols, A,C, G and T, are allowed. Each sequence has length at least 2 and at most 100.

Print exactly one line for each test case. The line is to contain the lexicographically smallest sequence for the test case.

The following shows sample input and output for two test cases.

2 CGAGTCAGCT CTCC

AGCTCGAGTC CCCT



0%