周赛总结(11.13)

来源:互联网 发布:网络医院预约挂号系统 编辑:程序博客网 时间:2024/06/05 10:29

1.Encoding

Problem Description
Given a string containing only 'A' - 'Z', we could encode it using the following method:

1. Each sub-string containing k same characters should be encoded to "kX" where "X" is the only character in this sub-string.

2. If the length of the sub-string is 1, '1' should be ignored.
 

Input
The first line contains an integer N (1 <= N <= 100) which indicates the number of test cases. The next N lines contain N strings. Each string consists of only 'A' - 'Z' and the length is less than 10000.
 

Output
For each test case, output the encoded string in a line.
 

Sample Input
2ABCABBCCC
 

Sample Output
ABCA2B3C
代码示例:
#include<stdio.h>#include<string.h>char a[10005];int main(){    int n;    scanf("%d",&n);    while(n--)    {        int i,la;        scanf("%s",a);        la=strlen(a);        for(i=0; i<la;)        {            int k,s=1;            for(k=i; k<=la; k++)            {                if(a[k]!=a[k+1])                    break;                s++;            }            if(s>1)                printf("%d",s);            printf("%c",a[k]);            i+=s;        }        printf("\n");    }    return 0;}

ps:英文题,没理解题意直接懵逼了。。差不多所有都试了,就是不知道题意是5B3E3B这样,,还以为只能输出一个相同字符,所以说理解题意很重要!

2.Write a simple HTML Browser

Problem Description
If you ever tried to read a html document on a Macintosh, you know how hard it is if no Netscape is installed.Now, who can forget to install a HTML browser? This is very easy because most of the times you don't need one on a MAC because there is a Acrobate Reader which is native to MAC. But if you ever need one, what do you do?Your task is to write a small html-browser. It should only display the content of the input-file and knows only the html commands (tags) <br> which is a linebreak and <hr> which is a horizontal ruler. Then you should treat all tabulators, spaces and newlines as one space and display the resulting text with no more than 80 characters on a line.
 
Input
The input consists of a text you should display. This text consists of words and HTML tags separated by one or more spaces, tabulators or newlines.A word is a sequence of letters, numbers and punctuation. For example, "abc,123" is one word, but "abc, 123" are two words, namely "abc," and "123". A word is always shorter than 81 characters and does not contain any '<' or '>'. All HTML tags are either <br> or <hr>.
 
Output
You should display the the resulting text using this rules:  . If you read a word in the input and the resulting line does not get longer than 80 chars, print it, else print it on a new line.  . If you read a <br> in the input, start a new line.   . If you read a <hr> in the input, start a new line unless you already are at the beginning of a line, display 80 characters of '-' and start a new line (again).The last line is ended by a newline character.
 
Sample Input
Hallo, dies ist eine ziemlich lange Zeile, die in Htmlaber nicht umgebrochen wird.<br>Zwei <br> <br> produzieren zwei Newlines. Es gibt auch noch das tag <hr> was einen Trenner darstellt.Zwei <hr> <hr> produzieren zwei Horizontal Rulers.Achtung mehrere Leerzeichen irritierenHtml genauso wenig wiemehrere Leerzeilen.
 

Sample Output
Hallo, dies ist eine ziemlich lange Zeile, die in Html aber nicht umgebrochenwird.Zweiproduzieren zwei Newlines. Es gibt auch noch das tag--------------------------------------------------------------------------------was einen Trenner darstellt. Zwei----------------------------------------------------------------------------------------------------------------------------------------------------------------produzieren zwei Horizontal Rulers. Achtung mehrere Leerzeichen irritieren Htmlgenauso wenig wie mehrere
代码示例:
#include<stdio.h>#include<string.h>int main(){    char s[100010][85];    int len,i=0,cnt=0,k,j;    while(scanf("%s",s[i])!=EOF)        i++;    k=i;    for(i=0; i<k; i++)    {        if(!strcmp(s[i],"<br>"))        {            printf("\n");            cnt=0;        }        else if(!strcmp(s[i],"<hr>"))        {            if(cnt)                printf("\n");            for(j=0; j<80; j++)                printf("-");            printf("\n");            cnt=0;        }        else        {            len=strlen(s[i]);            if(!cnt)            {                printf("%s",s[i]);                cnt=len;            }            else if(cnt+len+1>80)            {                printf("\n%s",s[i]);                cnt=len;            }            else            {                printf(" %s",s[i]);                cnt+=len+1;            }        }    }    printf("\n");    return 0;}

ps:好吧,我承认这题真心不会,不过还不错,起码见识到了二维字符数组的应用。。
3.Shuffle'm Up

Description

A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several different colors.

The actual shuffle operation is performed by interleaving a chip from S1 with a chip from S2 as shown below for C = 5:

The single resultant stack, S12, contains 2 * C chips. The bottommost chip of S12 is the bottommost chip from S2. On top of that chip, is the bottommost chip from S1. The interleaving process continues taking the 2nd chip from the bottom of S2 and placing that on S12, followed by the 2nd chip from the bottom of S1 and so on until the topmost chip from S1 is placed on top of S12.

After the shuffle operation, S12 is split into 2 new stacks by taking the bottommost C chips from S12 to form a new S1 and the topmost C chips from S12 to form a new S2. The shuffle operation may then be repeated to form a new S12.

For this problem, you will write a program to determine if a particular resultant stack S12 can be formed by shuffling two stacks some number of times.

Input

The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that follow.

Each dataset consists of four lines of input. The first line of a dataset specifies an integer C, (1 ≤ C ≤ 100) which is the number of chips in each initial stack (S1 and S2). The second line of each dataset specifies the colors of each of the C chips in stack S1, starting with the bottommost chip. The third line of each dataset specifies the colors of each of the C chips in stack S2 starting with the bottommost chip. Colors are expressed as a single uppercase letter (A through H). There are no blanks or separators between the chip colors. The fourth line of each dataset contains 2 * Cuppercase letters (A through H), representing the colors of the desired result of the shuffling of S1 and S2 zero or more times. The bottommost chip’s color is specified first.

Output

Output for each dataset consists of a single line that displays the dataset number (1 though N), a space, and an integer value which is the minimum number of shuffle operations required to get the desired resultant stack. If the desired result can not be reached using the input for the dataset, display the value negative 1 (−1) for the number of shuffle operations.

Sample Input

24AHAHHAHAHHAAAAHH3CDECDEEEDDCC

Sample Output

1 22 -1

代码示例:

#include<stdio.h>#include<string.h>#include<iostream>#include<map>using namespace std;int main(){    int t,m=0;    scanf("%d",&t);    while(++m<=t)    {        int k,i;        scanf("%d",&k);        getchar();        char s1[201],s2[201],s12[401];//牌堆s1 牌堆s2  最终的牌堆状态s12        scanf("%s %s %s",s1,s2,s12);        map<string,bool>vist;//记录出现过的洗牌状态(map缺省值为0)        vist[s12]=true;        int step=0;//洗牌次数        while(true)        {            char s[201];//当前s1与s2洗牌后的状态            int ps=0;//s[]指针             for(i=0;i<k;i++)            {                s[ps++]=s2[i];//s1与s2洗牌                s[ps++]=s1[i];//是从s2先开始的            }            s[ps]='\0';//末尾结束            step++;            if(!strcmp(s,s12))//当洗牌后的状态能达到预设状态时,输出            {                printf("%d %d\n",m,step);                break;            }            else if(vist[s])//当前洗牌后状态 与 前面出现过的状态重复了,但该状态不是预设状态              {                printf("%d -1\n",m);//说明预设的状态无法达到                 break;            }            vist[s]=true;//状态记录             for(i=0;i<k;i++)//分拆出s1与s2              {                s1[i]=s[i];                s2[i]=s[i+k];            }            s1[i]='\0';            s2[i]='\0';        }    }    return 0;}

ps:这题也是比较好的,学到了STL,但是map还是不太明白,还需多回顾,多锻炼。。

4.最少拦截系统

Problem Description
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹.
怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统.
 

Input
输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔)
 

Output
对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统.
 

Sample Input
8 389 207 155 300 299 170 158 65
 

Sample Output
2
代码示例1:
#include<stdio.h>int a[1000];int main(){    int n,t;    while(~scanf("%d",&t))    {        int tot,i,j;        scanf("%d",&n);        a[0]=n,tot=1;        for(i=1;i<t;i++)        {            scanf("%d",&n);            for(j=0;j<tot;j++)            {                if(n<=a[j])                {                    a[j]=n;                    break;                }            }            if(j>=tot)                a[tot++]=n;        }        printf("%d\n",tot);    }    return 0;}


代码示例2:
#include<stdio.h>//**dp[i]表示第i个导弹飞过来时需要的最少拦截装置.**//  int main()  {      int n,i,j,max,h[10001],dp[10001];      while(~scanf("%d",&n))      {          max=-1;          dp[0]=0;          for(i=1;i<=n;i++)          {              scanf("%d",&h[i]);//**飞来的高度**//              dp[i]=1;//**初始化拦截装置都为1**//          }          for(i=1;i<=n;i++)          {              for(j=i-1;j>=0;j--)              {                  if(h[i]>h[j]&&dp[i]<dp[j]+1)//**如果在拦截中出现了非单调递减的**//                  {                      dp[i]=dp[j]+1;                  }              }          }          for(i=1;i<=n;i++)          {              if(dp[i]>max)              {                  max=dp[i];              }          }          printf("%d\n",max);      }      return 0;  }  

ps:学到了好多方法,这两种思路都很妙。。

1 0
原创粉丝点击