CF1569A Balanced Substring 題解
阿新 • • 發佈:2021-11-13
題意
給出 \(T\) 個長度為 \(n\) 且僅含 \(\texttt{a}\) , \(\texttt{b}\) 的字串,對於每個字串,輸出任意的 \(l\) ,\(r\) ,表示字串的第 \(l\) 個字元到第 \(r\) 中 \(\texttt{a}\) , \(\texttt{b}\) 字元的數量相同。
\(\texttt{SOLUTION}\)
記字串為 \(s\) ,字串中的第 \(x\) 個字元為 \(s_x\) 。
不難發現,如果字串中存在 \(s_x \ne s_{x-1}\) ( \(2\le x\le n\) )那麼直接輸出 \(x-1\) , \(x\) 即可。
如果不存在任意一對 \(s_x \ne s_{x-1}\) ( \(2\le x\le n\) ),則字串中只可能全為 \(\texttt{a}\) 或全為 \(\texttt{b}\) ,這樣肯定無解。
於是……
就\(\texttt{AC}\)了。
\(\texttt{AC CODE}\)
#include<bits/stdc++.h> using namespace std; const int MAX=100010; int read() { int x=0; char ch=getchar(); while(ch<'0'||ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return x; } int n; char str[MAX]; bool check() { for(int i=2;i<=n;i++) { if(str[i]^str[i-1]) return printf("%d %d\n",i-1,i),1; } return 0; } int main() { int t=read(); while(t--) { scanf("%d%s",&n,str+1); if(!check()) puts("-1 -1"); } return 0; }