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.
InputThe 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 di (1?≤?di?≤?109) — the length of the jump from thei-th cell.
OutputPrint "INFINITE" (without quotes) if grasshopper will continue his jumps forever. Otherwise print "FINITE" (without quotes).
ExamplesInput2><1 2OutputFINITEInput3>><2 1 1OutputINFINITENoteIn the first sample grasshopper starts from the first cell and jumps to the right on the next cell. When he is in the second cell he needs to jump two cells left so he will jump out of the strip.
Second sample grasshopper path is 1 - 3 - 2 - 3 - 2 - 3 and so on. The path is infinite.
題目大意:
現在有一個1*N的地方,每個格子都有一個方向鍵,表示走到這個位子的時候需要往哪個方向走,走的長度為di.
問是否能夠走出這個地方,從左邊出去或者右邊出去都算出去。
思路:
對于不能出去的情況,其實就是出現了某個格子走過兩次的時候。如果對于一個格子來講走過了第一次還要走第二次,那么肯定再走下去還是會回到這個位子,會再次走環路。
那么我們設定一個數組vis【i】表示位子i是否已經走過了,如果已經走過了又走了上去,那么就是肯定無法出去的情況。相反,如果某一時刻走了出去,就是能夠走出去的情況。過程維護一下就行了。
Ac代碼:
#include<stdio.h>#include<string.h>using namespace std;char a[1000000];int vis[1000000];int d[1000000];int main(){ int n; while(~scanf("%d",&n)) { scanf("%s",a+1); memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++)scanf("%d",&d[i]); int flag=0; int now=1; while(now<=n&&now>=1) { //printf("%d/n",now); if(vis[now]==1) { flag=0; break; } vis[now]=1; if(a[now]=='<')now-=d[now]; else now+=d[now]; if(now>n||now<1) { flag=1; break; } } if(flag==1)printf("FINITE/n"); else printf("INFINITE/n"); }}
新聞熱點
疑難解答