A:思维
题意:给定一个字符串,问是否是若干个Yes的子串
思路:我们用一个单串“Yes”去扫描字符串,看是否能够依次匹配。注意,字符串的开头部分需要特殊处理一下。具体看代码:
代码:
#include<bits/stdc++.h>
#define int long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N=2e5+10;
typedef pair<int,int>PII;
inline void solve(){
string s;cin>>s;
if(s[0]!='Y'&&s[0]!='e'&&s[0]!='s'){//特判
cout<<"NO\n";return;
}
//特殊化处理
if(s[0]=='e') s="Y"+s;
else if(s[0]=='s') s="Ye"+s;
int n=s.size();
s="*"+s;
string a="Yes";//单串
bool ok=false;
for(int i=1,l=0;i<=n;i++){
if(a[l]!=s[i]){
cout<<"NO\n";return;
}
l++;
if(l==3) l=0;
}
cout<<"YES\n";
}
signed main(){
int T;cin>>T;
while(T--) solve();
}
B: 枚举
题意:定义了一个序列:该序列中的数为1-n,且只能出现一次。给定了一个长度为n的残缺数组,给定了丢失数字的和。问能否还原一个序列?
思路:先通过和,找出丢失的数,再判断最终的序列是否为定义的序列。
代码:
#include<bits/stdc++.h>
#define int long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N=2e5+10;
typedef pair<int,int>PII;
inline void solve(){
map<int,int>mp;
int n,s;cin>>n>>s;
int maxx=0;
for(int i=1;i<=n;i++){
int x;cin>>x;
mp[x]++;
maxx=max(maxx,x);
}
int sum=0;
for(int i=1;i<=s;i++){
if(mp.count(i)) continue;
sum+=i;mp[i]++;
maxx=max(maxx,i);
if(sum>=s) break;
}
if(sum==s){
for(int i=1;i<=maxx;i++){
if(!mp.count(i)){
cout<<"NO\n";return;
}
}
cout<<"YES\n";
}
else cout<<"NO\n";
}
signed main(){
int T;cin>>T;
while(T--) solve();
}
C:
此题来自:Codeforces Round #834 (Div. 3) A - G - 知乎
代码:
#include<bits/stdc++.h>
#define int long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N=2e5+10;
typedef pair<int,int>PII;
inline void solve(){
int l,r,x,a,b;cin>>l>>r>>x>>a>>b;
int ans;
if(a==b) ans=0;
else if(abs(a-b)>=x) ans=1;
else{
if(b-l>=x and r-b>=x){
if(a-l>=x or r-a>=x) ans=2;
else ans=-1;
}
else if(b-l>=x){
if(a-l>=x) ans=2;
else if(r-a>=x) ans=3;
else ans=-1;
}
else if(r-b>=x){
if(r-a>=x) ans=2;
else if(a-l>=x) ans=3;
else ans=-1;
}
else ans=-1;
}
cout<<ans<<"\n";
}
signed main(){
int T;cin>>T;
while(T--) solve();
}
D:
代码:
#include<bits/stdc++.h>
#define int long long
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N=2e5+10;
typedef pair<int,int>PII;
inline void solve(){
int n, m;cin >> n >> m;
int cnt2 = 0, cnt5 = 0, x = n;
while(x % 2 == 0) x /= 2, cnt2++;
while(x % 5 == 0) x /= 5, cnt5++;
int ans = 0;
for(int i = min(cnt2, cnt5); ; i++){
int t = 1;
for(int j = 0; j < i - cnt2; j++) t *= 2;
for(int j = 0; j < i - cnt5; j++) t *= 5;
if(t > m) break;
ans = m / t * t * n;
}
cout << ans << endl;
}
signed main(){
int T;cin>>T;
while(T--) solve();
}