Educational Codeforces Round 157 (Rated for Div. 2) C. Torn Lucky Ticket

#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
#define int long long
typedef pair<int,int> pii;

const int N=200010;

int pre[N][10];
string s[N];
int sum[N];
int a[10][100];
void solve()
{
	int n;
	cin>>n;
	memset(a,0,sizeof a);
	memset(pre,0,sizeof pre);
	memset(sum,0,sizeof sum); 
	for(int i=1;i<=n;i++)
	{
		cin>>s[i];
		s[i]=' '+s[i];
		int sum1=0,cnt1=s[i].size()-1;
		for(int j=1;j<=cnt1;j++)
		{
			sum1+=s[i][j]-'0';
			pre[i][j]=pre[i][j-1]+s[i][j]-'0';
		}
		a[cnt1][sum1]++;
		sum[i]=sum1;
    } 
    int ans=0;
    for(int i=1;i<=n;i++)
    {
    	int len=s[i].size()-1;
    	for(int j=0;len-2*j>=0;j++)
    	{
    		//cout<<len-j<<" "<<sum[i]-2*pre[i][j]<<endl;
    		if(sum[i]-2*pre[i][j]>=0)ans+=a[len-2*j][sum[i]-2*pre[i][j]];
		}
		//cout<<"++"<<endl;
		for(int j=len-1;2*j-len>=0;j--)                                                         
		{
			if(2*pre[i][j]-sum[i]>=0)ans+=a[2*j-len][2*pre[i][j]-sum[i]];
		}
		//cout<<endl;
	//	cout<<endl;
	}
	cout<<ans<<endl;
    
}														                                                                                                  
signed main()
{ 
    int good_luck_to_you;
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    good_luck_to_you=1; 
   // cin>>good_luck_to_you;
    while(good_luck_to_you--)
    {
       solve();
    }
    system("pause");   
}

本题的大意是两个字符串拼接在一起,然后可以分成两半,前后两半的和相同。

思路:思路就是枚举每一个字符串,然后把每个字符串当成是拼接后的前缀和后缀,符合条件的记录下来就好。思路简单,不过就是从哪里开始循环,哪里结束,要想清楚,不然会有重复的情况,最好画一下图模拟一下,会很清楚,这道题记录的原因是很考验耐性,用二维数组来记录每一个字符串的位数和和,在遍历字符串的符合条件的直接加入答案即可。 

 

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值