Educational Codeforces Round 81 (Rated for Div. 2)
http://codeforces.com/contest/1295
A. Display The Number
思路:首先位数越多越好,那么直接不断的填1,但是只剩3段可以点亮时在首位填个7比较好
#include<bits/stdc++.h>
using namespace std;
int t,n;
string s;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
s = "";
while(n >= 2 && n != 3)
s+='1',n-=2;
if(n == 3)
s = '7' + s;
cout << s << endl;
}
return 0;
}
B. Infinite Prefixes
思路:最终要满足 i y + a = x iy+a=x iy+a=x,其中y为一个周期的贡献,a为周期中可以得到的贡献,x已知,枚举a看i是否存在即可
#include<bits/stdc++.h>
#define MAXN 100005
using namespace std;
int t,n,x;
char s[MAXN];
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d%s",&n,&x,s);
int y = 0;
for(int i = 0;i < n;++i)
{
if(s[i] == '0')
++y;
else
--y;
}
int ans = 0;
int tmp = 0;
bool flag = false;
for(int i = 0;i < n;++i)
{
if(x - tmp == 0)
flag = true;
if(y!= 0 && (x-tmp)%y == 0 && (x-tmp)/y >= 0)
++ans;
if(s[i] == '0')
++tmp;
else
--tmp;
}
if(y == 0 && flag)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}
C. Obtain The String
思路:首先贪心,s中字符每次拿越多越好,所以就枚举t中的字符ch看s此时是否可以拿出字符和ch匹配(一轮中拿出的字符是s的子序列),至于是否可以拿出字符和ch匹配是一个套路,先把s中的每个字符出现的下标记录下来即可
#include<bits/stdc++.h>
#define MAXN 100005
using namespace std;
char s[MAXN],t[MAXN];
vector<int> ve[26];
int T;
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%s%s",s,t);
for(int i = 0;i < 26;++i)
ve[i].clear();
int len1 = strlen(s);
for(int i = 0;i < len1;++i)
ve[s[i]-'a'].push_back(i);
int ans = 1;
int len2 = strlen(t);
int tmp = 0;
bool flag = true;
for(int i = 0;i < len2;++i)
{
int ch = t[i]-'a';
if(ve[ch].size() == 0)
{
flag = false;
break;
}
int id = lower_bound(ve[ch].begin(),ve[ch].end(),tmp)-ve[ch].begin();
if(id != ve[ch].size())
tmp = ve[ch][id]+1;
else
++ans,tmp = 0,--i;
}
if(flag)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}
D. Same GCDs
见https://blog.csdn.net/xing_mo/article/details/104111794