题解:CodeForces 641A Little Artem and Grasshopper[模拟]
CodeForces 641A
A. Little Artem and Grasshopper
time limit per test: 2 seconds
memory limit per test: 256 megabytes
inputstandard input
outputstandard output
Little Artem found a grasshopper. He brought it to his house and constructed a jumping area for him.
The area looks like a strip of cells \(1 × n\). Each cell contains the direction for the next jump and the length of that jump. Grasshopper starts in the first cell and follows the instructions written on the cells. Grasshopper stops immediately if it jumps out of the strip. Now Artem wants to find out if this will ever happen.
Input
The first line of the input contains a single integer \(n (1 ≤ n ≤ 100 000)\) — length of the strip.
Next line contains a string of length n which consists of characters "<" and ">" only, that provide the direction of the jump from the corresponding cell. Next line contains \(n\) integers \(d_i (1 ≤ d_i ≤ 10^9)\) — the length of the jump from the \(i\)-th cell.
Output
Print "INFINITE" (without quotes) if grasshopper will continue his jumps forever. Otherwise print "FINITE" (without quotes).
Examples
input 1
2
><
1 2
output 1
FINITE
input 2
3
>><
2 1 1
output 2
INFINITE
我的题解
很简单,从第一个点按照题目逻辑开始暴力
用一个map存储走过的点
只要第二次走到走过的点就不行
假如落在范围之外就可以
我的代码
#include <bits/stdc++.h>
#define int long long
int t,n;
std::string s
std::map<int,int> mp;
void solve()
{
std::cin >> n;
std::vector<int> a(n);
std::cin >> s;
for(int i = 0 ; i < n ; i ++) std::cin >> a[i];
int st = 0;
while(st >= 0 && st < n)
{
if(mp[st])
{
std::cout << "INFINITE";
return;
}
mp[st]++;
if(s[st] == '>') st += a[st];
else st -= a[st];
}
std::cout << "FINITE";
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
solve();
return 0;
}
有什么出现纰漏的地方还请大家在评论区指出!谢谢!
posted on 2024-07-14 14:33 Jiejiejiang 阅读(4) 评论(0) 编辑 收藏 举报