河南多校暑期训练-恢复赛 <字典树,最短路,queue,map,模拟,思维>


A - Babelfish
Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output

cat
eh
loops

Hint

Huge input and output,scanf and printf are recommended.


字符输入的恶心---

字典树代码:

#include<cstdio>
#include<map>
#include<cstring>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
    char ch1[200],ch2[200];
struct node{
    struct node * hai[26];
    int kp;
    char ch[12];
};
node *trie=new node();
void  build(char xx[])
{
    int sh,ll=strlen(xx);
    node * kk= trie;
    for (int i=0;i<ll;i++)
    {
        sh=xx[i]-'a';
        if (!kk->hai[sh])
            kk->hai[sh]=new node();
        kk=kk->hai[sh];
    //    printf("%d   iiii\n",i);
    }
    kk->kp=1;
    strcpy(kk->ch,ch1);
}
int cha(char xx[])
{
    int sh,ll=strlen(xx);
    node * kk= trie;
    for (int i=0;i<ll;i++)
    {
        sh=xx[i]-'a';
        if (!kk->hai[sh])
            return false;
        kk=kk->hai[sh];
    }
  //  printf("llllll\n");
    if (!kk->kp)
        return false;
    strcpy(ch2,kk->ch);
    return true;
}
int main()
{
    char  qian;
    while (qian=getchar())
    {
        if (qian=='\n')
            break;
        scanf("%s%s",ch1,ch2);
        int ll=strlen(ch1);
        for (int i=ll+1;i>0;i--)
        ch1[i]=ch1[i-1];
        ch1[0]=qian;
        build(ch2);
        getchar();

    }
   // printf("lll\n");
        while (scanf("%s",ch1)!=EOF)
        {

       //     printf("%s   66\n",ch1);
            if (cha(ch1))
            {
                printf("%s\n",ch2);
            }
            else
            {
                printf("eh\n");
            }
            getchar();
        }
    return 0;
}



sscanf() - 从一个 字符串 中读进与指定格式相符的数据。

输入好美——-爱上它了

开始用map不会输出--用cout时没想起加《iostream》

map代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<map>
#include<iostream>
using namespace std;
int main()
{
    map < string ,string > ma;
    char ch[50],ch1[50],ch2[50];
    while (gets(ch))
    {
        if (ch[0]==0)
            break;
        sscanf(ch,"%s%s",ch1,ch2);//重读 
        ma[ch2]=ch1;
    }
    while (gets(ch1))
        if (ma[ch1]!="")
            printf("%s\n",ma[ch1].c_str());
   // 或cout<<ma[ch1]<<endl;
    else
        printf("eh\n");
    return 0;
}


B - The Pilots Brothers' refrigerator
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.

There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.

The task is to determine the minimum number of handle switching necessary to open the refrigerator.

Input

The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.

Output

The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

Sample Input

-+--
----
----
-+--

Sample Output

6
1 1
1 3
1 4
4 1
4 3
4 4


好题呀---

当想只改变【x,y】的状态时,可以在【x , y】所在点的同列和同行的所有点都按一下--然后就只有【x,y】的状态变了--(因为行数和列数都是偶数)

然后将改变的次数压缩一下就行了


代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
    char ma[5][5];
    for (int i=0;i<4;i++)
        scanf("%s",ma[i]);
    int zeng[5][5];

    memset(zeng,0,sizeof(zeng));
    for (int i=0;i<4;i++)
    for (int j=0;j<4;j++)
    if (ma[i][j]=='+')
    {
        for (int ii=0;ii<4;ii++)
        zeng[ii][j]++;
        for (int ii=0;ii<4;ii++)
        zeng[i][ii]++;
        zeng[i][j]--;
    }
    for (int i=0;i<4;i++)
        for (int j=0;j<4;j++)
        zeng[i][j]%=2;
    int s=0;
    for (int i=0;i<4;i++)
        for (int j=0;j<4;j++)
        if (zeng[i][j])
        s++;
    printf("%d\n",s);
    for (int i=0;i<4;i++)
        for (int j=0;j<4;j++)
        if (zeng[i][j])
        printf("%d %d\n",i+1,j+1);
    return 0;
}




F - Frogger
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimming and instead reach her by jumping. 
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps. 
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence. 
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones. 

You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone. 

Input

The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy's stone, stone #2 is Fiona's stone, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.

Output

For each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.

Sample Input

2
0 0
3 4

3
17 4
19 4
18 5

0

Sample Output

Scenario #1
Frog Distance = 5.000

Scenario #2
Frog Distance = 1.414


---求可联通路中的最大路(的尽可能的小值)

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
double x[220],y[220],lu[220][220],dis[220],mimi;
int n,ca;
double li(int ii,int jj)
{
    double lp;
    lp=sqrt((x[ii]-x[jj])*(x[ii]-x[jj])+(y[ii]-y[jj])*(y[ii]-y[jj]));
    return lp;
}
void disk()
{
    bool fafe[220];
    memset(fafe,true,sizeof(fafe));
    for (int i=1;i<=n;i++)
        dis[i]=lu[1][i];
    fafe[1]=false;
    mimi=0;
    while (1)
    {
        int ii;int mi=1000000;
        for (int i=1;i<=n;i++)
            if (fafe[i]&&dis[i]<mi)
        {
            ii=i;
            mi=dis[i];
        }
    //    printf("%lf\n",dis[2]);
        if (ii==2)
        {
            printf("Scenario #%d\n",ca++);
            printf("Frog Distance = %.3lf\n\n",dis[2]);
            break;
        }
        fafe[ii]=false;
        //dis[2]=min(dis[2],lu[ii][2]);
        for (int i=1;i<=n;i++)
        if (fafe[i])
        {
            dis[i]=min(dis[i],max(dis[ii],lu[ii][i]));
        }
    }
}
int main()
{
    ca=1;
    while (scanf("%d",&n),n)
    {
        for (int i=1;i<=n;i++)
            scanf("%lf%lf",&x[i],&y[i]);
        int kp=0;
        memset(lu,0x3f3f3f,sizeof(lu));
        for (int i=1;i<n;i++)
        for (int j=i+1;j<=n;j++)
        {
            lu[i][j]=lu[j][i]=li(i,j);
        }
        disk();
    }
    return 0;
}

G - Catch That Cow
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points - 1 or + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers:  N and  K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.


无奈---开始用结构体一直超时---让我怀疑人生了---

感觉有一种二进制的方法---但有点难处理===

代码:

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
int bu[200201];
int main()
{
    int n,k;

    memset(bu,0,sizeof(bu));
    scanf("%d%d",&n,&k);
    queue<int > que;
    if (n==k)
    {
        printf("0\n");
        return 0;
    }
    if (n>k)
    {
        printf("%d\n",n-k);
        return 0;
    }
    que.push(n);bu[n]=1;
    int qian,now;
    int mi=k-n+1;
    while (!que.empty())
    {
        now=que.front();//printf("%d    66\n",now);
        que.pop();
        if (now==k)
        {
             break;
        }

        if (mi<=bu[now])
        {
            bu[k]=mi;
            break;
        }
        if (now<k)
        {
            if (!bu[2*now])
            {
                qian=2*now;
                bu[qian]=bu[now]+1;
        //        printf("%d   %d\n",qian,bu[qian]);
                que.push(qian);
            }
            if (!bu[now+1])
            {
                qian=now+1;
                bu[qian]=bu[now]+1;
   //                             printf("%d   %d\n",qian,bu[qian]);

                que.push(qian);
            }
            if (!bu[now-1]&&now-1>=0)
            {
                qian=now-1;
                bu[qian]=bu[now]+1;

                que.push(qian);
            }
        }
        else
        {
            mi=min(mi,bu[now]+now-k);
        }
    }
    printf("%d\n",bu[k]-1);
    return 0;
}



I - Cow Bowling
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this: 

          7 

 3   8 

 8   1   0 

 2   7   4   4 

 4   5   2   6   5
Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame. 

Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.

Input

Line 1: A single integer, N 

Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.

Output

Line 1: The largest sum achievable using the traversal rules

Sample Input

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

Sample Output

30

Hint

Explanation of the sample: 

          7 
         *

 3   8 
       *

 8   1   0 
       *

 2   7   4   4 
       *

 4   5   2   6   5
The highest score is achievable by traversing the cows as shown above.

简单的dp--

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int shu[400][400];
int he[400][400];
int main()
{
    int n;
    while (~scanf("%d",&n))
    {
        memset(he,0,sizeof(he));
        memset(shu,0,sizeof(shu));
        for (int i=1;i<=n;i++)
            for (int j=1;j<=i;j++)
            scanf("%d",&shu[i][j]);
        for (int i=n;i>0;i--)
            for (int j=1;j<=i;j++)
            he[i][j]=max(he[i+1][j],he[i+1][j+1])+shu[i][j];
        printf("%d\n",he[1][1]);
    }
    return 0;
}


J - Robot Motion
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu

Description


A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are 

N north (up the page) 
S south (down the page) 
E east (to the right on the page) 
W west (to the left on the page) 

For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid. 

Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits. 

You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around. 

Input

There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.

Output

For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.

Sample Input

3 6 5
NEESWE
WWWESS
SNWWWW
4 5 1
SESWE
EESNW
NWEEN
EWSEN
0 0 0

Sample Output

10 step(s) to exit
3 step(s) before a loop of 8 step(s)

模拟水过---

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char ma[1000][1000];
/*struct node{
    int x,y;
    int shu;
}now,qian;*/
int bu[1000][1000],x,y,ans;
int main()
{
    int n,m,kai;
    while (scanf("%d%d%d",&n,&m,&kai),n+m+kai)
    {
        memset(bu,0,sizeof(bu));
        for (int i=0;i<n;i++)
            scanf("%s",ma[i]);
        kai--;
        bu[0][kai]=1;
        x=0;y=kai;ans=1;
        int kkkk=1;
        while (kkkk)
        {
         //   printf("%d  %d\n",x,y);
            if (ma[x][y]=='W')
            {
                y--;
            }
            else if (ma[x][y]=='E')
            {
                y++;
            }
            else if (ma[x][y]=='N')
            {
                x--;
            }
            else
            {
                x++;
            }
            if (x<0||y<0||x==n||y==m)
            {
                printf("%d step(s) to exit\n",ans);
                break;
            }
            if (bu[x][y])
            {
                printf("%d step(s) before a loop of %d step(s)\n",bu[x][y]-1,ans-bu[x][y]+1);
                break;
            }
            ans++;
            bu[x][y]=ans;

        }
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值