当前位置: 代码迷 >> 综合 >> Kattis Trik
  详细解决方案

Kattis Trik

热度:81   发布时间:2023-12-12 09:32:15.0

此文章可以使用目录功能哟↑(点击上方[+])

 Kattis <Trik>

Accept: 0    Submit: 0
Time Limit: 1 second    Memory Limit : 1024 MB

 Problem Description

Jealous of Mirko’s position as head of the village, Borko stormed into his tent and tried to demonstrate Mirko’s incompetence for leadership with a trick.

Borko puts three opaque cups onto the table next to each other (opening facing down) and a small ball under the leftmost cup. He then swaps two cups in one of three possible ways a number of times. Mirko has to tell which cup the ball ends up under.


Wise Mirko grins with his arms crossed while Borko struggles to move the cups faster and faster. What Borko does not know is that programmers in the back are recording all his moves and will use a simple program to determine where the ball is. Write that program.

 Input

The first and only line contains a non-empty string of at most 50 characters, Borko’s moves. Each of the characters is ‘A’, ‘B’ or ‘C’ (without quote marks).

 Output

Output the index of the cup under which the ball is: 1 if it is under the left cup, 2 if it is under the middle cup or 3 if it is under the right cup.

 Sample Input

AB
CBABCACCC

 Sample Output

3
1

 Problem Idea

解题思路:

【题意】

桌上倒扣着三个不透明的杯子

最初,最左边的杯子内有一个小球

问经过一系列的位置交换,最终小球在哪个杯子内

交换过程用字符串表示,'A'表示左边的杯子与中间的杯子交换;'B'表示中间的杯子与右边的杯子交换;'C'表示左边的杯子与右边的杯子交换

【类型】
模拟

【分析】

由于字符串长度最大为50,意味着交换次数最多50次

那我们可以直接进行模拟

以样例1为例:




所以,小球最终在右边的杯子内,故输出"3"

【时间复杂度&&优化】
O(strlen(s))

题目链接→Kattis <Trik>

 Source Code

/*Sherlock and Watson and Adler*/
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<complex>
#include<string>
#include<algorithm>
#include<iostream>
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define bitnum(a) __builtin_popcount(a)
using namespace std;
const int N = 55;
const int M = 100005;
const int inf = 1000000007;
const int mod = 10007;
char s[N];
int main()
{int a=1,b=0,c=0,i;scanf("%s",s);for(i=0;s[i]!='\0';i++)if(s[i]=='A')swap(a,b);else if(s[i]=='B')swap(b,c);elseswap(a,c);if(a)puts("1");else if(b)puts("2");elseputs("3");return 0;
}
菜鸟成长记