Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes

 Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes

题意:有n个01串,可以任意交换串内或串和串间的字符。问最多能获得多少回文串。

思路:记录奇奇串的个数为oo,记录偶偶串的个数为ee,记录奇偶或偶奇的个数为oe,然后如果oo是偶数,那么奇奇串每两个之间可以交换变成一个回文串,如果oo为奇数,那么如果oe存在,那么还是可以交换所有的为回文串;如果不存在答案就是n-1

(因为把one写成了zero,于是WA了n次,弄得我都觉得自己错了,看着别人博客推翻自己,结果还是觉得自己思路是对的,最后还要靠别人给我找出这个无比小的bug,我哭了)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) x & (-x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 55 + 5;
 
struct node{
    string s;
    int one, zero;
    node(int a = 0, int b = 0):one(a), zero(b){}
};
int main()
{
    int q; scanf("%d", &q);
    while(q -- )
    {
        node a[maxN];
        int n; scanf("%d", &n); getchar();
        for(int i = 0; i < n; i ++ )
        {
            cin >> a[i].s;
            int siz = a[i].s.size();
            for(int j = 0; j < siz; j ++ )
            {
                if(a[i].s[j] == '0')
                    a[i].zero ++;
                else
                    a[i].one ++;
            }
        }
        int oo = 0, ee = 0, eo = 0;
        for(int i = 0; i < n; i ++ )
        {
            if(a[i].one & 1 && a[i].zero & 1)
                oo ++;
            else if(!(a[i].one & 1) && !(a[i].zero & 1))
                ee ++;
            else
                eo ++;
        }
        int ans = n;
        if( oo & 1 && !eo )
            ans -- ;
        printf("%d\n", ans);
    }
    return 0;
}

 

 

 

别人的思路: 01串的四种情况

  1.  0奇,1奇
  2.  0偶,1偶
  3.  0奇,1偶
  4.  0偶,1奇

只有第一种情况不能自身构成回文串,如果要变成回文串,可以奇奇交换,也可以奇奇和奇偶或偶奇交换。

如果只有情况1、2,那么01总数必须都是偶数才能所有的都构成回文串

如果只有情况1,那么01总数必须都是偶数才能所有的都构成回文串

如果有情况3或4那么总可以把情况1转换成回文串

只有情况1、2没有情况3、4并且01个数都为奇数的时候,总有一个01串不能构成回文串。【其实跟我的思路也差不多,只是我面向的是不同串的个数,而他是面向01的个数和是否有奇偶串】

 

啊啊啊 好难想www 回宿舍了 溜了溜了 明天排位了www

 

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) x & (-x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 55 + 5;
 
int n;
 
int main()
{
    int q; scanf("%d", &q);
    while(q -- )
    {
        int one = 0, zero = 0, flag = 0;
        scanf("%d", &n);
        for(int i = 0; i < n; i ++ )
        {
            string s;
            cin >> s;
            int len = s.size();
            int tzero = 0, tone = 0;
            for(int j = 0; j< len; j ++ )
            {
                if(s[j] == '0')
                    tzero ++;
                else
                    tone ++;
            }
            if((tzero + tone) & 1)
                flag = 1;
            one += tone;
            zero += tzero;
        }
        if(one & 1 && zero & 1 && !flag)
            n -- ;
        printf("%d\n", n);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值