Description
Input
Output
Sample Input
dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
Sample Output
cat eh loops
Hint
字符输入的恶心---
字典树代码:
#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;
}
输入好美——-爱上它了
开始用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;
}
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;
}
Description
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
Output
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;
}
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 X - 1 or X + 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
Output
Sample Input
5 17
Sample Output
4
Hint
无奈---开始用结构体一直超时---让我怀疑人生了---
感觉有一种二进制的方法---但有点难处理===
代码:
#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;
}
Description
7 3 8 8 1 0 2 7 4 4 4 5 2 6 5Then 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
Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.
Output
Sample Input
5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
Sample Output
30
Hint
7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5The 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;
}
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
Output
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;
}