原题链接
题意
把一个01串分成字串,最小化所有子串的
M
E
X
MEX
MEX (没有出现过的最小非负整数)。
思路
- 如果都是1的话,肯定是0
- 如果都是0,肯定是1
- 如果有1有0
- 如果0只有1段,那么就还是1,使那一段0单独为1个子串即可
- 如果0不止一段,最多也只可能是2,使整个字符串为一串
代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t -- )
{
string s;
cin >> s;
string ss = s;
sort(ss.begin(), ss.end());
// if (s[0] == '1') cout << "0" << endl;
if (ss[0] == '0' && ss[s.length() - 1] == '0')
{
cout << "1" << endl;
continue;
}
else if (ss[0] == '1' && ss[s.length() - 1] == '1')
{
cout << "0" << endl;
continue;
}
int a = 0, b = 0;
s += "2";
for (int i = 0; i < s.length() - 1; i ++ )
{
if (s[i] != s[i + 1] && s[i] == '0')
{
a ++;
}
else if (s[i] != s[i + 1] && s[i] == '1')
{
b ++;
}
if (a > 1) break;
}
if (a == 1) cout << 1 << endl;
else cout << 2 << endl;
// else if (s[0] == '0' && s[s.length() - 1] == '1') cout << "2" << endl;
}
return 0;
}
总结
写的时候稍微想了一会儿,但是不难。