JNOJ 1101 travel

1101: Travel

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 126   Solved: 22
[ Submit][ Status][ Web Board]

Description

One day, Doraemon have words with his friend Nobi and decided to take a travel. He found that there are n locations label from 1-n in a line. Each location have a machine that can get person to anywhere he like. These machines are powered by two companies. The bosses of the companies are jealous about each other and decide that you can travel to the machine that owned by himself for free but have to pay abs(st-en) if the endpoint is belong to the other’s company. Now Doraemon is in the location a and he wants you to calculate the minimum cost for him to go to location b.


Input

The first line contains a single integer t, the number of cases.

In every input, the first line contains a single integer n(1<=n<=10000), the second line contains a string s contains whose length is n and contains only 0 and 1. If s[i]=0, then machine i is belong to the first company, otherwise it belongs to the second company. The third line contains two integers a and b, indicates the start point and end point of Doraemon.


Output

A single line, indicates the minimum cost of Doraemon.


Sample Input

1
5
10101
1 2

Sample Output

1

HINT

Source


#include<cstdio>
#include<iostream>
#include<algorithm>

using namespace std;

int main(){
	int t=0;
	int n=0;
	int i=0;
	int fee=0;
	int st,en;
	char a[10000];
	cin>>t;
	while(t--){
		fee=10000;
		cin>>n;
		cin>>a;
		cin>>st>>en;
		//cout<<a<<endl;
		if(a[st-1]==a[en-1])cout<<0<<endl;
		else{
			if(st<en){
				for(i=st-1;i<en;i++)
				if(a[i]!=a[st-1]){
					fee=i-st+1;
					//cout<<fee<<endl;
					break;
				}
			for(i=st-1;i>=0;i--)
				if(a[i]!=a[st-1]){
					if(st-1-i<fee)
					fee=st-1-i;
					//cout<<fee<<endl;
					break;
				}
			
			}
			else{
				st^=en;
				en^=st;
				st^=en;
				for(i=en-1;i>=st-1;i--)
				if(a[i]!=a[en-1]){
					if(en-1-i<fee)
						fee=en-1-i;
					//cout<<fee<<endl;
					break;
				}
			for(i=en-1;i<n;i++)
				if(a[i]!=a[en-1]){
					if(i-en+1<fee)
						fee=i-en+1;
					//cout<<fee<<endl;
					break;
				}
				
			}
			
			cout<<fee<<endl;
		}
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值