Educational Codeforces Round 81 (Rated for Div. 2)


链接

签到,简单贪心。可以看出来只会使用最多一个7剩下的全是1

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;


int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		
		if (n%2==0)
		{
			for (int i = 0; i < n/2; ++i)
			{
				cout<<1;
			}
			cout<<endl;
		}
		else
		{
			cout<<7;
			for (int i = 0; i < (n-3)/2; ++i)
			{
				cout<<1;
			}
		}
		cout<<endl;
	}
	
	return 0;
}

/*
0 要4
1 要2
2 要3
3 要5
4 要4
5 要5
6 要6
7 要3
8 要7
9 要6
*/

链接

预处理前缀的值,对于任意一个前缀,肯定是N个串s加上一个s的前缀,遍历即可

    #include<bits/stdc++.h>

using namespace std;

typedef long long ll;

char a[100010];
int num[100010];
int main(int argc, char const *argv[])
{
	//ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
		int n,q;
		cin>>n>>q;
		cin>>a;
		memset(num,0,sizeof(num));
		for (int i = 0; i < n ; ++i)
		{
			if (a[i]=='1')
			{
				num[i+1]=num[i]-1;
			}
			else
			{
				num[i+1]=num[i]+1;
			}
		}
		int k=num[n]; //也就是一个圈能增加多少
		//后面的肯定是与这个同余的
		ll ans = 0;
		ll gap = 0;
		if(k==0)
		{
			//cout<<"gg"<<endl;
			gap = q-num[0]; //算出来了差距
			if(gap==0)
			{
				ans++;
			}
			for (int i = 1; i <= n; ++i)
			{
				gap = q-num[i]; //算出来了差距
				if (gap==k)
				{
					ans++;
					break;
				}

			}
			
			if (ans>0)
			{
				cout<<-1<<endl;
			}
			else
				cout<<0<<endl;
		}
		else 
		{
			gap = q-num[0]; //差距
			if (gap==0)
			{
				ans++;
			}
			for (int i = 1; i <= n; ++i)
			{
				gap = q-num[i]; //差距
				if (gap/k>=0 && gap%k==0)
				{
					ans++;
				}

			}
			cout<<ans<<endl;
		}
		
	}

	return 0;
}

链接

贪心,建立跳转表来快速定位

#include<bits/stdc++.h>

using namespace std;

const int N = 2e5+10;
const int INF = 1e9+10;

typedef long long ll;

int pos[N][26];

char a[N];
char b[N];

int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--)
	{
		cin>>a;
		cin>>b;
		ll ans = 1;
		for (int i = 0; i < N; ++i)
		{
			for (int j = 0; j < 26; ++j)
			{
				pos[i][j] = INF;
			}
		}
		for (int i = strlen(a)-1; i >=0; --i)
		{
			  for (int j = 0; j < 26; ++j)
			  {
			  	if (j==(a[i]-'a'))
			  	{
			  		pos[i][j] = i;
			  	}
			  	else
			  	{
			  		pos[i][j] = pos[i+1][j];
			  	}
			  }
		}
		int flag = 0;
		int p=0;
		for (int i = 0; i < strlen(b); ++i)
		{
			if (p==strlen(a)) //超出去就回到0,是为了满足最后一个还能满足
			{
					p=0;
					ans++;
					//cout<<ans<<endl;
			}
			if (pos[p][b[i]-'a'] == INF) //后面已经不存在当前这个情况了,必须要重新开始找了。
			{
				
				ans++;
				p=0;
				//cout<<ans<<endl;
			}
			if (p==0&&pos[p][b[i]-'a']==INF) //判断没有出现
			{
				flag = 1;
				break;
			}
			p = pos[p][b[i]-'a']+1; //加1的目的是,万一还是当前这个,肯定不能记录两次

		}
		if(flag)
		{
			cout<<-1<<endl;
		}
		else
			cout<<ans<<endl;
	}
	return 0;
}
/*
3
aabce
ace
abacaba
aax
ty
yyt
*/
posted @ 2020-01-31 20:11  CrosseaLL  阅读(106)  评论(0编辑  收藏  举报