Problem A: (More) Multiplication
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 100 Solved: 54
[ Submit][ Status][ Web Board]
Description
An example would be when multiplying 345 * 56 = 19320 as given below, using a lattice grid with 2 rows and 3 columns, which appears inside a surrounding frame:
| 3 4 5 |
| +---+---+---+ |
| |1 /|2 /|2 /| |
| | / | / | / |5|
|1|/ 5|/ 0|/ 5| |
| +---+---+---+ |
|/|1 /|2 /|3 /| |
| | / | / | / |6|
|9|/ 8|/ 4|/ 0| |
| +---+---+---+ |
|/ 3 / 2 / 0 |
+---------------+
|3 /|
| / |
|/ 0|
+---+
The overall product is then computed by summing along the diagonals in the lattice that represent the same place values in the result. For example, in our first problem the product 19320 was computed as:
1's digit | = 0 |
10's digit | = 5 + 3 + 4 = 12, thus 2 with a carry of 1 |
100's digit | = (1 carry) + 2 + 0 + 2 + 8 = 13, thus 3 with a carry of 1 |
1000's digit | = (1 carry) + 2 + 5 + 1 = 9 |
10000's digit | = 1 |
To provide an aesthetic view, we use a series of minus (-) characters for horizontal lines, pipe (|) characters for vertical lines, and slash (/) characters for diagonal lines. Furthermore, we use a plus (+) character wherever a horizontal and vertical line meet. Each multiplication lattice is subsequently "boxed" by an outer border. There is a row containing the first operand which is between the topmost border and the top line of the grid, and a row between the bottom of the grid and the bottom border, which contains some portion of the resulting product. There is one column between the leading | and the left edge of the inner grid, which may contain a portion of the resulting product, and one column after the right edge of the inner grid but before the rightmost | border, which contains the second operand. If the product is not long enough to wrap around the bottom-left corner, the column between the left border and the left edge of the grid will containing only spaces. (See the later example of 3 x 3.)
Leading zeros should be displayed within lattice grid cells, but leading zeros should never be displayed in the product, nor should there ever be a slash (/) character prior to the leading digit of the product. For example, consider the product of 12 * 27 = 324 below:
| 1 2 |
| +---+---+ |
| |0 /|0 /| |
| | / | / |2|
| |/ 2|/ 4| |
| +---+---+ |
| |0 /|1 /| |
| | / | / |7|
|3|/ 7|/ 4| |
| +---+---+ |
|/ 2 / 4 |
+-----------+
Input
The input contains one or more tests. Each test contains two positive integers, A and B, such that 1 ≤ A ≤ 9999 and 1 ≤ B ≤ 9999. The last data set will be followed by a line containing 0 0.
Output
For each data set, produce the grid that illustrates how to multiply the two numbers using the lattice multiplication technique.
Sample Input
345 56
12 27
1 68
9999 7
3 3
0 0
Sample Output
+---------------+
| 3 4 5 |
| +---+---+---+ |
| |1 /|2 /|2 /| |
| | / | / | / |5|
|1|/ 5|/ 0|/ 5| |
| +---+---+---+ |
|/|1 /|2 /|3 /| |
| | / | / | / |6|
|9|/ 8|/ 4|/ 0| |
| +---+---+---+ |
|/ 3 / 2 / 0 |
+---------------+
+-----------+
| 1 2 |
| +---+---+ |
| |0 /|0 /| |
| | / | / |2|
| |/ 2|/ 4| |
| +---+---+ |
| |0 /|1 /| |
| | / | / |7|
|3|/ 7|/ 4| |
| +---+---+ |
|/ 2 / 4 |
+-----------+
+-------+
| 1 |
| +---+ |
| |0 /| |
| | / |6|
| |/ 6| |
| +---+ |
| |0 /| |
| | / |8|
|6|/ 8| |
| +---+ |
|/ 8 |
+-------+
+-------------------+
| 9 9 9 9 |
| +---+---+---+---+ |
| |6 /|6 /|6 /|6 /| |
| | / | / | / | / |7|
|6|/ 3|/ 3|/ 3|/ 3| |
| +---+---+---+---+ |
|/ 9 / 9 / 9 / 3 |
+-------------------+
+-------+
| 3 |
| +---+ |
| |0 /| |
| | / |3|
| |/ 9| |
| +---+ |
| 9 |
+-------+
#include<stdio.h>
#include<string.h>
typedef long long ll;
int main(){
int a,b,i,j,h,t,q,p;
int n1[10],n2[10];
while(~scanf("%d%d",&a,&b)){
if(a==0&&b==0) break;
ll c=a*b;
memset(n1,0,sizeof(n1)); memset(n2,0,sizeof(n2));
int l1=0,l2=0;
//l1求出来的是a的位数,l2求出来的是b的位数;
while(a){
n1[l1++]=a%10;
a=a/10;
}
while(b){
n2[l2++]=b%10;
b=b/10;
}
//k代表的是一共有几个数字;
int sum[17]={0},k=0,ll1=l1-1,ll2=l2-1;
for(i=l2-1;i>=0;i--){
for(j=l1-1;j>=0;j--){
sum[k++]=n2[i]*n1[j];
}
}
int g[11111]={0},zn=0;
ll ctemp=c;
//for(i=0;i<k;i++) printf("%d ",sum[i]); puts("");
//先求出c有几位;
while(ctemp){
zn++;
ctemp=ctemp/10;
}
ctemp=c;
for(i=zn;i>=1;i--){
g[i]=ctemp%10;
ctemp=ctemp/10;
}
//for(i=1;i<=zn;i++) printf("%d ",g[i]); puts("");
t=0;
int tnum=zn,flag=0; //因为是从最后一位输出;flag 标记是否需要输到竖行去;
if(zn>l1) flag=1;
printf("+-");
for(i=1;i<=4*l1+1;i++) printf("-");
printf("-+\n");
//printf("+---------------+\n");
printf("| %d",n1[ll1--]);
for(i=ll1;i>=0;i--) printf(" %d",n1[i]);
printf(" |\n");
if(l1!=1){
for(i=1;i<=l1;i++){
if(i==1) printf("| +---+");
else if(i!=l1&&i!=1) printf("---+");
else if(i==l1) printf("---+ |\n");
}
}
else if(l1==1){
printf("| +---+ |\n");
}
int len1=l1-1,len2=l2-1,count1=0;
//flag 的作用是为了判断当前的那个数字要不要加到列上去;
if(flag){
count1=l2-(zn-l1);
}
//printf("%d\n",count1);
//h2数组是用来储存个位数的;
int fout=0;
int h1,h2[66]={0},count=1,flag2=0,tcont=1,flag3=0;
//下面是对总共在高度上一共有l2个晶体块;
//i代表的就是高度;
#if 1
for(i=1;i<=l2;i++){
t=0; memset(h2,0,sizeof(h2));
//对晶体块的高度进行循环;
//main:
if(flag){
if(fout<count1) flag2=0;
else flag2=1;
}
if(fout>count1) flag3=1;
for(p=0;p<3;p++){
q=0;
if(p==0){
if(!flag3) printf("| ");
else printf("|/");
for(j=0;j<l1;j++){
h2[j]=sum[(i-1)*l1+j]%10;
sum[(i-1)*l1+j]/=10;
if(j!=l1-1) printf("|%d /",sum[(i-1)*l1+j]);
else printf("|%d /|",sum[(i-1)*l1+j]);
}
printf(" |\n");
}
if(p==1){
printf("| ");
for(j=0;j<l1;j++){
if(j!=l1-1) printf("| / ");
else printf("| / |");
}
printf("%d|\n",n2[len2--]);
}
if(p==2){
if(flag2) printf("|%d",g[tcont++]);
else printf("| ");
for(j=0;j<l1;j++){
if(j!=l1-1) printf("|/ %d",h2[j]);
else printf("|/ %d|",h2[j]);
}
printf(" |\n");
}
}
printf("| ");
for(p=1;p<=l1;p++){
if(p!=l1) printf("+---");
else printf("+---+");
}
printf(" |\n");
if(flag) fout++;
}
int nn=1;
if(flag==0){
printf("| ");
for(i=1;i<=l1-zn;i++) printf(" ");
for(i=1;i<=l1;i++){
if(i!=l1) printf(" %d /",g[i]);
else printf(" %d |\n",g[i]);
}
}
else if(flag){
printf("|/");
for(i=tcont;i<=zn;i++){
if(i!=zn) printf(" %d /",g[i]);
else printf(" %d |\n",g[i]);
}
}
printf("+-");
for(i=1;i<=4*l1+1;i++) printf("-");
printf("-+\n");
#endif
}
}
Problem B: Fun House
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 221 Solved: 82
[ Submit][ Status][ Web Board]
Description
American Carnival Makers Inc. (ACM) has a long history of designing rides and attractions. One of their more popular attractions is a fun house that includes a room of mirrors. Their trademark is to set up the room so that when looking forward from the entry door, the exit door appears to be directly ahead. However, the room has double-sided mirrors placed throughout at 45 degree angles. So, the exit door can be on any of the walls of the room. The set designer always places the entry and mirrors, but can never seem to be bothered to place the exit door. One of your jobs as part of the construction crew is to determine the placement of the exit door for the room given an original design.
The final diagram for a sample room is given below. The asterisk (*) marks the entry way, lower case x's mark the walls, the mirrors are given by the forward and backward slash characters (/ and \), open spaces with no visual obstructions are marked by periods (.), and the desired placement of the exit is marked with an ampersand (&). In the input diagram, there is an 'x' in place of the '&', since the exit has not yet been located. You need to alter the input diagram by replacing the proper 'x' with an '&' to identify the exit. Note that entrances and exits can appear on any of the walls (although never a corner), and that it is physically impossible for the exit to be the same as the entrance. (You don't need to understand why this is so, although it may be fun to think about.)
xxxxxxxxxxx
x../..\...x
x..../....x
*../......x
x.........x
xxxxxx&xxxx
Input
Each room will be preceded by two integers, W and L, where 5 ≤ W ≤ 20 is the width of the room including the border walls and 5 ≤ L ≤ 20 is the length of the room including the border walls. Following the specification of W and L are L additional lines containing the room diagram, with each line having W characters from the alphabet: { * , x , . , / , \ }. The perimeter will always be comprised of walls, except for one asterisk (*) which marks the entrance; the exit is not (yet) marked. A line with two zeros indicates the end of input data.
Output
For each test case, the first line will contain the word, HOUSE, followed by a space and then an integer that identifies the given fun house sequentially. Following that should be a room diagram which includes the proper placement of the exit door, as marked by an ampersand (&).
Sample Input
11 6
xxxxxxxxxxx
x../..\...x
x..../....x
*../......x
x.........x
xxxxxxxxxxx
5 5
xxxxx
*...x
x...x
x...x
xxxxx
5 5
xxxxx
x./\x
*./.x
x..\x
xxxxx
6 6
xxx*xx
x/...x
x....x
x/./.x
x\./.x
xxxxxx
10 10
xxxxxxxxxx
x.../\...x
x........x
x........x
x.../\..\x
*...\/../x
x........x
x........x
x...\/...x
xxxxxxxxxx
0 0
Sample Output
HOUSE 1
xxxxxxxxxxx
x../..\...x
x..../....x
*../......x
x.........x
xxxxxx&xxxx
HOUSE 2
xxxxx
*...&
x...x
x...x
xxxxx
HOUSE 3
xxxxx
x./\x
*./.x
x..\&
xxxxx
HOUSE 4
xxx*xx
x/...x
x....x
x/./.&
x\./.x
xxxxxx
HOUSE 5
xxxxxxxxxx
x.../\...x
x........x
x........x
&.../\..\x
*...\/../x
x........x
x........x
x...\/...x
xxxxxxxxxx
这道题也是一道模拟题,题意大致是:
#include<stdio.h>
#include<string.h>
int w,l,x0,y0;
char a[55][55];
void print(){
for(int i=0;i<l;i++) printf("%s\n",a[i]);
}
//向右走,
void goright(int tx,int ty){
void goup(int tx,int ty);
void godown(int tx,int ty);
void goleft(int tx,int ty);
int i,j,k,f1=0,f2=0,f3=0,f4=0,flag=1;
for(i=ty+1;i<w;i++){
if(a[tx][i]=='/') {flag=0; f4=1; break;}
if(a[tx][i]==92) {flag=0; f2=1; break;}
}
if(f2) godown(tx,i);
if(f4) goup(tx,i);
if(flag) {a[tx][w-1]='&'; print(); }
}
//向上走;
void goup(int tx,int ty){
void godown(int tx,int ty);
void goleft(int tx,int ty);
int i,j,k,f1=0,f2=0,f3=0,f4=0,flag=1;
for(i=tx-1;i>=0;i--){
if(a[i][ty]=='/') {flag=0; f3=1; break;}
if(a[i][ty]==92) {flag=0; f1=1; break;}
}
if(f1) goleft(i,ty);
if(f3) goright(i,ty);
if(flag) {a[0][ty]='&'; print(); }
}
//向下走;
void godown(int tx,int ty){
void goleft(int tx,int ty);
int i,j,k,f1=0,f2=0,f3=0,f4=0,flag=1;
for(i=tx+1;i<l;i++){
if(a[i][ty]=='/') {flag=0; f1=1; break;}
if(a[i][ty]==92) {flag=0; f3=1; break;}
}
if(f1) goleft(i,ty);
if(f3) goright(i,ty);
if(flag) {a[l-1][ty]='&'; print(); }
}
//向左走;
void goleft(int tx,int ty){
int i,j,k,f1=0,f2=0,f3=0,f4=0,flag=1;
for(i=ty-1;i>=0;i--){
if(a[tx][i]=='/') {flag=0; f2=1; break;}
if(a[tx][i]==92) {flag=0; f4=1; break;}
}
if(f2) godown(tx,i);
if(f4) goup(tx,i);
if(flag) {a[tx][0]='&'; print(); }
}
int main(){
int i,j,k=1;
while(~scanf("%d%d",&w,&l)){
if(w==0&&l==0) break;
for(i=0;i<l;i++) scanf("%s",a[i]);
for(i=0;i<l;i++){
for(j=0;j<w;j++){
if(a[i][j]=='*') {x0=i; y0=j; break;}
}
}
//先判断它是在哪个墙上;
//1: 它是在第一行上;
printf("HOUSE %d\n",k++);
if(x0==0){
int flag=1,f1=0,f2=0,f3=0,f4=0;
for(i=0;i<l;i++){
if(a[i][y0]=='/') {f1=1; flag=0; break;}
if(a[i][y0]==92) {f3=1; flag=0; break;}
}
if(flag==1) {a[l-1][y0]='&'; print(); continue;}
if(f1) goleft(i,y0);
if(f3) goright(i,y0);
continue;
}
//2: 它在最后一行上,那么只能向上走;
if(x0==l-1){
int flag=1,f1=0,f2=0,f3=0,f4=0;
for(i=l-1;i>=0;i--){
if(a[i][y0]=='/') {f3=1; flag=0; break;}
if(a[i][y0]==92) {f1=1; flag=0; break;}
}
if(flag==1) {a[0][y0]='&'; print(); continue;}
if(f1) goleft(i,y0);
if(f3) goright(i,y0);
continue;
}
//3: 它在最左边的一列,那么只能往右走;
if(y0==0){
int flag=1,f1=0,f2=0,f3=0,f4=0;
for(i=0;i<w;i++){
if(a[x0][i]=='/') {f4=1; flag=0; break;}
if(a[x0][i]==92) {f2=1; flag=0; break;}
}
if(flag==1) {a[x0][w-1]='&'; print();continue;}
//printf("%d %d\n",f2,f4);
if(f2) godown(x0,i);
if(f4) goup(x0,i);
continue;
}
//4: 它在最右边的一列,那么只能往左走;
if(y0==w-1){
int flag=1,f1=0,f2=0,f3=0,f4=0;
for(i=w-1;i>=0;i--){
if(a[x0][i]=='/') {f2=1; flag=0; break;}
if(a[x0][i]==92) {f4=1; flag=0; break;}
}
if(flag==1) {a[x0][0]='&'; print();continue;}
if(f2) godown(x0,i);
if(f4) goup(x0,i);
continue;
}
}
}
Problem C: Lexicography
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 169 Solved: 54
[ Submit][ Status][ Web Board]
Description
An anagram of a string is any string that can be formed using the same letters as the original. (We consider the original string an anagram of itself as well.) For example, the string ACM has the following 6 anagrams, as given in alphabetical order:
ACM
AMC
CAM
CMA
MAC
MCA
As another example, the string ICPC has the following 12 anagrams (in alphabetical order):
CCIP
CCPI
CICP
CIPC
CPCI
CPIC
ICCP
ICPC
IPCC
PCCI
PCIC
PICC
Given a string and a rank K, you are to determine the Kth such anagram according to alphabetical order.
Input
Each test case will be designated on a single line containing the original word followed by the desired rank K. Words will use uppercase letters (i.e., A through Z) and will have length at most 16. The value of K will be in the range from 1 to the number of distinct anagrams of the given word. A line of the form "# 0" designates the end of the input.
Output
For each test, display the Kth anagram of the original string.
Sample Input
ACM 5
ICPC 12
REGION 274
# 0
Sample Output
MAC
PICC
IGNORE
HINT
The value of K could be almost 245 in the largest tests, so you should use type long in Java, or type long long in C++ to store K.
这道题又是一道类似于一道想法题的题目,一开始我想的很简单,直接用STL中的next_permutation就好了,但是结果却TLE了。所以只能靠想法了。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
char a[50],cnew[50];
ll vis[50],f[50]={0};
int main(){
int i,j,l,len,tlen=0;
f[0]=1;
//这里是为了求阶乘,十分方便;
for(i=1;i<=16;i++){
f[i]=f[i-1]*i;
}
ll k;
while(~scanf("%s%lld",a,&k)){
len=strlen(a);
if(len==1&&a[0]=='#'&&k==0) break;
memset(vis,0,sizeof(vis));
for(i=0;i<len;i++){
vis[a[i]-'A']++;
}
sort(a,a+len);
//for(i=0;i<26;i++) printf("%d ",vis[i]); puts("");
#if 1
for(i=0;i<len;i++){
//循环一遍26个字母;
ll last=0;
for(j=0;j<26;j++){
if(vis[j]){
//ans代表的是无重复的时候全排列的数量;
ll ans=f[len-1-i];
for(l=0;l<26;l++){
if(l==j) ans=ans/f[vis[l]-1];
else ans=ans/f[vis[l]];
}
//如果这个条件成立的话,那么就break,进行到下一位去就好了;
if(last+ans>=k){
cnew[i]=j+'A';
k=k-last;
vis[j]--;
break;
}
else last+=ans;
}
}
}
cnew[len]='\0';
printf("%s\n",cnew);
#endif
}
}
Problem E: Word Cloud
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 79 Solved: 42
[ Submit][ Status][ Web Board]
Description
A word cloud (or tag cloud) is a visual representation of textual data based on a weighted metric. In the above cloud (which is based on this year's list of Mid-Central teams), the font size of each word is based on its number of occurrences in the data set. Tagg Johnson is a man obsessed with counting words that appear in online documents. On his computer, he keeps a spreadsheet of all the sites he visits, along with a list of words that appear on each site and the number of times such word appears. Tagg would like to generate word clouds based on the data he has collected.
Before describing the algorithm Tagg uses for generating clouds, we digress for a quick lesson in typography. The basic unit of measure is known as a point (typically abbreviated as pt). A font's size is described based on the vertical number of points from one line to the next, including any interline spacing. For example, with a 12pt font, the vertical space from the top of one character to the top of a character below it is 12 points. We assume that a character's height is precisely equal to the font's point size (regardless of whether the character is upper or lower case).
For this problem, we focus on a fixed-width font, such as Courier, in which each character of the alphabet is also given the same amount of width. The character width for such a font depends on the font size and the aspect ratio. For Courier, a word with t characters rendered in a font of size P has a total width of when measured in points. Note well the use of the ceiling operator, which converts any noninteger to the next highest integer. For example, a 5-letter word in a 20pt font would be rendered with a height of 20 points and a width equal to
points.
Now we can describe Tagg's algorithm for creating a word cloud. He pre-sorts his word list into alphabetical order and removes words that do not occur at least five times. For each word w, he computes a point size based on the formula , where cw is the number of occurrences of the word, and cmax is the number of occurrences of the most frequent word in the data set. Note that by this formula, every word will be rendered with anywhere from a 9pt font to a 48pt font. He then places the words in rows, with a 10pt horizontal space between adjacent words, placing as many words as fit in the row, subject to a maximum width W for his entire cloud. The height of a given row is equal to the maximum font size of any word rendered in that row.
As a tangible example, consider the following data set and word cloud.
Word Count
apple 10
banana 5
grape 20
kiwi 18
orange 12
strawberry 10
In this example, apple is rendered with 23pt font using width 65pt, banana is rendered with 11pt font using width 38pt, and grape is rendered with 48pt font and width 135pt. If the overall word cloud is constrained to have width at most 260, those three words fit in a row and the overall height of that row is 48pt (due to grape). On the second row kiwi is rendered with height 43pt and width 97pt, and orange is rendered with height 28pt and width 95pt. A third row has strawberry with height 23pt and width 130pt. The overall height of this word cloud is 114pt.
Input
Each data set begins with a line containing two integers: W and N. The value W denotes the maximum width of the cloud; W ≤ 5000 will be at least as wide as any word at its desired font size. The value 1 ≤ N ≤ 100 denotes the number of words that appear in the cloud. Following the first line are N additional lines, each having a string S that is the word (with no whitespace), and an integer C that is a count of the number of occurrences of that word in the original data set, with 5 ≤ C ≤ 1000. Words will be given in the same order that they are to be displayed within the cloud.
Output
For each data set, output the word CLOUD followed by a space, a serial number indicating the data set, a colon, another space, and the integer height of the cloud, measured in font points.
Sample Input
260 6
apple 10
banana 5
grape 20
kiwi 18
orange 12
strawberry 10
250 6
apple 10
banana 5
grape 20
kiwi 18
orange 12
strawberry 10
610 6
apple 10
banana 5
grape 20
kiwi 18
orange 12
strawberry 10
0 0
Sample Output
CLOUD 1: 114
CLOUD 2: 99
CLOUD 3: 48
#include<stdio.h>
#include<string.h>
#include<math.h>
char a[111][5555];
int c[5555],len[111];
int main(){
int w,n,i,j,k,nk=1;
while(~scanf("%d%d",&w,&n)){
if(w==0&&n==0) break;
memset(c,0,sizeof(c)); memset(len,0,sizeof(len));
int cmax=-1;
for(i=1;i<=n;i++){
scanf("%s%d",a[i],&c[i]);
len[i]=strlen(a[i]);
if(cmax<c[i]) cmax=c[i];
}
//printf("%d\n",cmax);
int p[111]={0},wid[111]={0};
for(i=1;i<=n;i++){
p[i]=(int)(ceil(8.0+40.0*(c[i]-4)/(cmax-4)));
wid[i]=(int)(ceil(9.0*len[i]*p[i]/16));
}
k=1;
int ctemp,num=0,slen=0,hsum=0;
int hmax=-1;
#if 1
//以单词的数目为循环控制条件;
while(num<n){
hmax=0;
//注意slen=wid[k];
slen=wid[k]; num++; hmax=p[k];
for(i=k+1;i<=n;i++){
if(slen+10+wid[i]<=w){
slen+=10+wid[i];
num++;
if(p[i]>hmax) hmax=p[i];
}
else {
ctemp=i; break;
}
}
hsum+=hmax;
k=ctemp;
}
printf("CLOUD %d: %d\n",nk++,hsum);
#endif
}
}
Problem G: Reverse Rot
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 160 Solved: 89
[ Submit][ Status][ Web Board]
Description
A very simplistic scheme, which was used at one time to encode information, is to rotate the characters within an alphabet and rewrite them. ROT13 is the variant in which the characters A-Z are rotated 13 places, and it was a commonly used insecure scheme that attempted to "hide" data in many applications from the late 1990's and into the early 2000's.
It has been decided by Insecure Inc. to develop a product that "improves" upon this scheme by first reversing the entire string and then rotating it. As an example, if we apply this scheme to string ABCD with a reversal and rotation of 1, after the reversal we would have DCBA and then after rotating that by 1 position we have the result EDCB.
Your task is to implement this encoding scheme for strings that contain only capital letters, underscores, and periods. Rotations are to be performed using the alphabet order:
ABCDEFGHIJKLMNOPQRSTUVWXYZ_.
Note that underscore follows Z, and the period follows the underscore. Thus a forward rotation of 1 means 'A' is shifted to 'B', that is, 'A'→'B', 'B'→'C', ..., 'Z'→'_', '_'→'.', and '.'→'A'. Likewise a rotation of 3 means 'A'→'D', 'B'→'E', ..., '.'→'C'.
Input
Each input line will consist of an integer N, followed by a string. N is the amount of forward rotation, such that 1 ≤ N ≤ 27. The string is the message to be encrypted, and will consist of 1 to 40 characters, using only capital letters, underscores, and periods. The end of the input will be denoted by a final line with only the number 0.
Output
For each test case, display the "encrypted" message that results after being reversed and then shifted.
Sample Input
1 ABCD
3 YO_THERE.
1 .DOT
14 ROAD
9 SHIFTING_AND_ROTATING_IS_NOT_ENCRYPTING
2 STRING_TO_BE_CONVERTED
1 SNQZDRQDUDQ
0
Sample Output
EDCB
CHUHKWBR.
UPEA
ROAD
PWRAYF_LWNHAXWH.RHPWRAJAX_HMWJHPWRAORQ.
FGVTGXPQEAGDAQVAIPKTVU
REVERSE_ROT
正宗的水题,要你转化一下字符串,28个为一个循环;
#include<stdio.h>
#include<string.h>
char a[44];
char b[33]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','_','.'};
void change(int len){
int i;
char c;
for(i=0;i<len/2;i++){
c=a[i]; a[i]=a[len-1-i]; a[len-1-i]=c;
}
}
int main(){
int n,i,j,k,len;
while(scanf("%d",&n)!=EOF){
if(n==0) break;
scanf("%s",a);
len=strlen(a);
change(len);
//printf("%s\n",a);
int lot=0;
for(i=0;i<len;i++){
for(j=0;j<28;j++){
if(a[i]==b[j]) {lot=j; break;}
}
lot=lot+n;
lot=lot%28;
a[i]=b[lot];
}
printf("%s\n",a);
}
}
Problem I: Wet Tiles
Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 119 Solved: 50
[ Submit][ Status][ Web Board]
Description
Alice owns a construction company in the town of Norainia, famous for its unusually dry weather. In fact, it only rains a few days per year there. Because of this phenomenon, many residents of Norainia neglect to do roof repairs until leaks occur and ruin their floors. Every year, Alice receives a deluge of calls from residents who need the leaks fixed and floor tiles replaced. While exquisite in appearance, Norainia floor tiles are not very water resistant; once a tile becomes wet, it is ruined and must be replaced. This year, Alice plans to handle the rainy days more efficiently than in past years. She will hire extra contractors to dispatch as soon as the calls come in, so hopefully all leaks can be repaired as soon as possible. For each house call, Alice needs a program to help her determine how many replacement tiles a contractor team will need to bring to complete the job.
For a given house, square floor tiles are arranged in a rectangular grid. Leaks originate from one or more known source locations above specific floor tiles. After the first minute, the tiles immediately below the leaks are ruined. After the second minute, water will have spread to any tile that shares an edge with a previously wet tile. This pattern of spreading water continues for each additional minute. However, the walls of a house restrict the water; if a damaged area hits a wall, the water does not penetrate the wall. We assume there are always four outer walls surrounding the entire house. A house may also have a number of additional "inner" walls; each inner wall is comprised of a connected linear sequence of locations (which may or may not be connected to the outer walls or to each other).
As an example, Figure 1 shows water damage (in gray) that would result from three initial leaks (each marked with a white letter 'L') after each of the first five minutes of time. Tiles labeled '2' become wet during the second minute, tiles labeled '3' become wet during the third minute, and so forth. The black areas designate inner walls that restrict the flow of water. Note that after 5 minutes, a total of 75 tiles have been damaged and will need to be replaced. Figures 2 through 4 show other houses that correspond to the example inputs for this problem.
75 wet tiles
17 wet tiles
4 wet tiles
94 wet tiles
Input
Each house is described beginning with a line having five integral parameters: X Y T L W. Parameters X and Y designate the dimensions of the rectangular grid, with 1 ≤ X ≤ 1000 and 1 ≤ Y ≤ 1000. The coordinate system is one-indexed, as shown in the earlier figures. Parameter T designates the number of minutes that pass before a team of contractors arrives at a house and stops the leaks, with 1 ≤ T ≤ 200000. The parameter L designates the number of leaks, with 1 ≤ L ≤ 100. Parameter W designates the number of inner walls in the house, 0 ≤ W ≤ 100.
The following 2L integers in the data set, on one or more lines, are distinct (x y) pairs that designate the locations of the L distinct leaks, such that 1 ≤ x ≤ X and 1 ≤ y ≤ Y.
If W > 0, there will be 4W additional integers, on one or more lines, that describe the locations of the walls. For each such wall the four parameters (x1,y1), (x2,y2) describe the locations of two ends of the wall. Each wall replaces a linear sequence of adjoining tiles and is either axis-aligned or intersects both axes at a 45 degree angle. Diagonal walls are modeled as a sequence of cells that would just be touching corner to corner. If the two endpoints of a wall are the same, the wall just occupies the single cell at that location. Walls may intersect with each other, but no leak is over a wall.
There will be one or more houses in the data file and a line with a single integer -1 designates the end of the data set.
Output
For each house, display the total number of tiles that are wet after T minutes.
Sample Input
12 12 5 3 5
2 11 3 3 9 5
1 9 6 9 1 7 4 4 7 1 7 4
10 9 10 12 11 4 12 4
9 7 8 1 3
4 3
2 2 6 6 6 2 2 6 8 2 8 2
6 7 50 1 3
3 4
2 2 2 6 3 6 5 4 5 4 3 2
12 12 5 3 0
2 11 3 3 9 5
-1
Sample Output
75
17
4
94
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int book[1111][1111];
int dic[4][2]={0,1,1,0,0,-1,-1,0};
struct node{
int x,y;
}que[1111111];
int main(){
int a,b,tt,l,w;
int i,j,k;
while(scanf("%d",&a)!=EOF&&(a!=-1)){
scanf("%d%d%d%d",&b,&tt,&l,&w);
memset(book,0,sizeof(book)); memset(que,0,sizeof(que));
for(i=1;i<=l;i++){
scanf("%d%d",&que[i].x,&que[i].y);
book[que[i].x][que[i].y]=1;
}
int x1,x2,y1,y2;
for(i=1;i<=w;i++){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1>x2) swap(x1,x2),swap(y1,y2);
//如果是在一列上的话;
if(y1==y2){
for(j=x1;j<=x2;j++) book[j][y1]=-1;
}
//如果是在一行上;
else if(x1==x2){
if(y1>y2) swap(y1,y2);
for(j=y1;j<=y2;j++) book[x1][j]=-1;
}
//如果既不是同一行又不是同一列;
else{
if(y1<y2){
for(j=0;j<=x2-x1;j++) book[x1+j][y1+j]=-1;
}
else for(j=0;j<=x2-x1;j++) book[x1+j][y1-j]=-1;
}
}
int h=1,t=l;
int xx,yy,tx,ty;
#if 1
while(h<=t){
xx=que[h].x;
yy=que[h].y;
if(book[xx][yy]>=tt) break;
h++;
for(k=0;k<4;k++){
tx=xx+dic[k][0];
ty=yy+dic[k][1];
if(tx>=1&&tx<=a&&ty>=1&&ty<=b&&book[tx][ty]==0){
book[tx][ty]=book[xx][yy]+1;
que[++t].x=tx;
que[t].y=ty;
}
}
}
#endif
printf("%d\n",t);
}
}
其实说实在的,这道题我自己想不太出来要怎么写下面的bfs。 但是没事,慢慢的总会好起来的吧。加油!