Balanced Substring
You are given a string ss, consisting of nn letters, each letter is either ‘a’ or ‘b’. The letters in the string are numbered from 11 to nn.
s[l;r]s[l;r] is a continuous substring of letters from index ll to rr of the string inclusive.
A string is called balanced if the number of letters ‘a’ in it is equal to the number of letters ‘b’. For example, strings “baba” and “aabbab” are balanced and strings “aaab” and “b” are not.
Find any non-empty balanced substring s[l;r]s[l;r] of string ss. Print its ll and rr (1≤l≤r≤n1≤l≤r≤n). If there is no such substring, then print −1−1 −1−1.
Input
The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of testcases.
Then the descriptions of tt testcases follow.
The first line of the testcase contains a single integer nn (1≤n≤501≤n≤50) — the length of the string.
The second line of the testcase contains a string ss, consisting of nn letters, each letter is either ‘a’ or ‘b’.
Output
For each testcase print two integers. If there exists a non-empty balanced substring s[l;r]s[l;r], then print ll rr (1≤l≤r≤n1≤l≤r≤n). Otherwise, print −1−1 −1−1.
Example
Input
4
1
a
6
abbaba
6
abbaba
9
babbabbaa
Output
-1 -1
1 6
3 6
2 5
Note
In the first testcase there are no non-empty balanced subtrings.
In the second and third testcases there are multiple balanced substrings, including the entire string “abbaba” and substring “baba”.
解释与代码
其实就是判断相邻的有没有不同,有不同就输出下标
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ppb pop_back
#define lbnd lower_bound
#define ubnd upper_bound
#define endl '\n'
#define trav(a, x) for(auto& a : x)
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define DEBUG cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x) cerr << #x << " is " << x << endl;
#define ini(a) memset(a,0,sizeof(a))
#define case ll T;read(T);for(ll Q=1;Q<=T;Q++)
#define lowbit(x) x&(-x)
#define pr printf
#define sc scanf
#define _ 0
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \
(void)(cout << "L" << __LINE__ \
<< ": " << #x << " = " << (x) << '\n')
#define TIE \
cin.tie(0);cout.tie(0);\
ios::sync_with_stdio(false);
//#define long long int
//using namespace __gnu_pbds;
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch ^ 48);
ch = getchar();
}
x *= f;
return;
}
inline void write(long long x) {
if(x<0) putchar('-'), x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
putchar('\n');
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 100909;
const ll N = 5;
char ch[10009];
int a, b;
void solve(){
int n;
cin>>n;
getchar();
gets(ch+1);
if (n==1) {
cout<<"-1 -1"<<endl;
return ;
} else {
for (int i=1; i<=n; i++) {
if (i<=n-1 && ch[i]!=ch[i+1]) {
cout<<i<<" "<<i+1<<endl;
return ;
}
}
}
cout<<"-1 -1"<<endl;
}
int main()
{
case{solve();}
}
Chess Tournament
A chess tournament will be held soon, where nn chess players will take part. Every participant will play one game against every other participant. Each game ends in either a win for one player and a loss for another player, or a draw for both players.
Each of the players has their own expectations about the tournament, they can be one of two types:
- a player wants not to lose any game (i. e. finish the tournament with zero losses);
- a player wants to win at least one game.
You have to determine if there exists an outcome for all the matches such that all the players meet their expectations. If there are several possible outcomes, print any of them. If there are none, report that it’s impossible.
Input
The first line contains a single integer tt (1≤t≤2001≤t≤200) — the number of test cases.
The first line of each test case contains one integer nn (2≤n≤502≤n≤50) — the number of chess players.
The second line contains the string ss (|s|=n|s|=n; si∈{1,2}si∈{1,2}). If si=1si=1, then the ii-th player has expectations of the first type, otherwise of the second type.
Output
For each test case, print the answer in the following format:
In the first line, print NO if it is impossible to meet the expectations of all players.
Otherwise, print YES, and the matrix of size n×nn×n in the next nn lines.
The matrix element in the ii-th row and jj-th column should be equal to:
- +, if the ii-th player won in a game against the jj-th player;
- -, if the ii-th player lost in a game against the jj-th player;
- =, if the ii-th and jj-th players’ game resulted in a draw;
- X, if i=ji=j.
Example
Input
3
3
111
2
21
4
2122
Output
YES
X==
=X=
==X
NO
YES
X--+
+X++
+-X-
--+X
解释与代码
我是把数据构造出来但不输出,i=j
的情况就是X
,1的情况就是=
2的情况就是先判断它ij
和ji
是不是都是空的,都是空的就+
最后反转过来
特殊情况就是2只有1个和2个的情况不行,我就是少了2个的情况
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define ppb pop_back
#define lbnd lower_bound
#define ubnd upper_bound
#define endl '\n'
#define trav(a, x) for(auto& a : x)
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (ll)x.size()
#define hell 1000000007
#define DEBUG cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x) cerr << #x << " is " << x << endl;
#define ini(a) memset(a,0,sizeof(a))
#define case ll T;read(T);for(ll Q=1;Q<=T;Q++)
#define lowbit(x) x&(-x)
#define pr printf
#define sc scanf
#define _ 0
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \
(void)(cout << "L" << __LINE__ \
<< ": " << #x << " = " << (x) << '\n')
#define TIE \
cin.tie(0);cout.tie(0);\
ios::sync_with_stdio(false);
//#define long long int
//using namespace __gnu_pbds;
template <typename T>
void read(T &x) {
x = 0;
int f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch ^ 48);
ch = getchar();
}
x *= f;
return;
}
inline void write(long long x) {
if(x<0) putchar('-'), x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
putchar('\n');
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int maxn = 100909;
const ll N = 5;
int n;
char ch[1009];
char cb[109][109];
int cntx[109];
int cnty[109];
void solve(){
ini(cntx);
ini(cnty);
int c1 = 0, c2 = 0;
cin>>n;
getchar();
gets(ch+1);
for (int i=1; i<=n; i++) {
if (ch[i] == '1') c1++;
else c2++;
}
if (c2 == 0) {
cout<<"YES"<<endl;
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
if (i == j) cout<<"X";
else cout<<"=";
}
cout<<endl;
}
} else if (c2 == 1 || c2 == 2){
cout<<"NO"<<endl;
} else {
cout<<"YES"<<endl;
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
if (i == j) {
cb[i][j] = 'X';
} else
cb[i][j] = '0';
}
}
int ccc = 0;
for (int i=1; i<=n; i++) {
if (ch[i] == '2')
for (int j=1; j<=n; j++) {
if (ch[j] == '1') continue;
if (ch[j] == '2' && cb[i][j] == '0' && cb[j][i] == '0') {
cb[i][j] = '+';
cntx[i]++;
break;
}
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
if (cb[i][j] == '0') {
if (cb[j][i] == '+'){
cb[i][j] = '-';
} else if (cb[j][i] == '-') {
cb[i][j] = '+';
}else{
cb[i][j] = '=';
}
}
}
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++) {
cout<<cb[i][j];
}
cout<<endl;
}
}
}
int main()
{
case{solve();}
}