A. Matching
分析
第一位为0一定不能没有匹配模板,分情况如果第一位为?乘9,其余为乘10。
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 100010,mod = 1e6+7;
typedef long long ll;
typedef pair<int,int> pii;
int n,m,k;
void solve()
{
string s;
cin >> s;
if(s[0] == '0')
{
cout << 0 << '\n';
return ;
}
int f = 0;
int cnt = 0;
for(int i = 0;i < s.size(); i++)
{
if(s[i] == '?')
{
if(i == 0) f = 1;
cnt++;
}
}
int ans = 0;
if(f)
{
if(cnt == 1) ans = 9;
else ans = 9 * pow(10,cnt - 1);
}
else
{
if(cnt == 0) ans = 0;
ans = pow(10,cnt);
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
T = 1;
cin >> T;
while(T--) solve();
return 0;
}
B. Sort the Subarray
分析
这个题我卡了好久,除了找b数组最长非递减序列,还要注意这段长度的a,b数组不同,哎,还是要把题通读几遍。
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 100010,mod = 1e6+7;
typedef long long ll;
typedef pair<int,int> pii;
int n,m,k;
void solve()
{
cin >> n;
vector<int> a(n),b(n);
for(int i = 0;i < n; i++) cin >> a[i];
for(int i = 0;i < n; i++) cin >> b[i];
int l = 1, r = 1;
int ans = 0;
for(int i = 0;i < n; i++)
{
int j = i;
int f = 0;
while(b[j] <= b[j+1] && j + 1 < n)
{
if(a[j] != b[j]) f = 1;
j++;
}
if(ans < j - i + 1 && f)
{
ans = j - i + 1;
l = i + 1, r = j + 1;
}
if(i != j) i = j - 1;
}
cout << l << ' ' << r << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
T = 1;
cin >> T;
while(T--) solve();
return 0;
}
C. Tear It Apart
分析
打的时候没想到正确做法,想的是保留字母个数最多的字母,模拟加贪心的删除字母,可后来发现贪错了。可行的贪法,就是假设26种字母某一个进行保留,删除其他字母考虑保留字母间的最长的个数取log2向上取整,思考下如果这个都能删除完,其余小于该长度的区间也能够操作完。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = 100010,mod = 1e9+7;
int n,m;
int cnt[30],l[30];
void solve()
{
string s;
cin >> s;
memset(cnt,0,sizeof cnt);
memset(l,-1,sizeof l);
for(int i = 0;i < s.size(); i++)
{
int x = s[i] - 'a';
cnt[x] = max(cnt[x], i - l[x] - 1);
l[x] = i;
}
int ans = 1e9;
for(int i = 0;i < 26; i++)
{
cnt[i] = max(cnt[i],(int)s.size() - l[i] - 1);
ans = min(ans,cnt[i]);
}
int res = 0;
while(ans)
{
ans /= 2;
res++;
}
cout << res << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int tt;
cin >> tt;
while(tt--)
{
solve();
}
return 0;
}