Vasya and Golden Ticket
原题链接
考察:双指针
思路:
枚举和k,求\(sum[r]-sum[l]==k\)的最大r,注意特判0
Code
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int n,sum[N];
char s[N];
bool check(int k)
{
int l = 0,cnt = 0,r = 0;
while(1)
{
while(r<=n&&sum[r]-sum[l]<=k) r++;
if(r-1==l||sum[r-1]!=sum[l]+k) return 0;
l = r-1;
cnt++;
if(l==n&&cnt>1) return 1;
else if(l==n&&cnt==1) return n>=2&&!k;
}
}
int main()
{
scanf("%d%s",&n,s+1);
for(int i=1;i<=n;i++)
{
int x = s[i]-'0';
sum[i] = sum[i-1]+x;
}
bool ok = 0;
for(int k=0;k<=900;k++)
{
if(!check(k)) continue;
ok = 1;
}
if(ok) puts("YES");
else puts("NO");
return 0;
}