ideone和Codeforces中相同程序的不同输出

时间:2015-02-26 09:58:11

标签: c++ dev-c++

对于问题http://codeforces.ru/contest/518/problem/B,我提交了解决方案。 这是解决方案的理想链接 - http://ideone.com/ucGGgA

在Ideone中,测试用例4的输出与Codeforces中的输出不同。

Input - 
zzzzz
ZZZZZ

Output-
in Ideone     - 0 5
In Codeforces - 5 0

编辑 - 这是解决方案 -

//#include<bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <stack>
#include <cmath>
#include <queue>
#include <string>
#include<climits>
#include<set>
#include<list>
#include<sstream>
using namespace std;

#define mod 1000000007
#define tr(c,i) for(auto i=(c).begin(); i != (c).end(); i++)
#define rep(i,n) for(int i=0;i<(n);i++)
#define forup(i,a,b) for(int i=(a);i<=(b);i++)
#define tcsolve() int tcs; cin >> tcs; forup(tc, 1, tcs) solve();
#define mp(a,b) make_pair(a,b)

inline void gi(int &x) {
    register int c = getchar();
    x = 0;
    int neg = 0;

    for (; ((c<48 || c>57) && c != '-'); c = getchar());

    if (c == '-') {
        neg = 1;
        c = getchar();
    }

    for (; c>47 && c<58; c = getchar()) {
        x = (x << 1) + (x << 3) + c - 48;
    }

    if (neg)
        x = -x;
}
inline void print(int a){ char s[20]; int i = 0; do{ s[i++] = a % 10 + '0'; a /= 10; } while (a); i--; while (i >= 0)putchar(s[i--]); putchar('\n'); }

void solve()
{
    string str1, str2, ans;
    int mp[1000];
    rep(i, 100)
    {
        mp[i] = 0;
    }
    cin >> str1 >> str2;
    int yay=0, woops=0;
    rep(i, str2.length())
    {
        mp[str2[i]]++;
    }
    rep(i, str1.length())
    {
        if (mp[str1[i]] > 0)
        {
            yay++;
            mp[str1[i]]--;
        }
        else
        {
            if (str1[i] >= 'a' && str1[i] <= 'z')
            {
                if (mp[str1[i] - 'a' + 'A'] > 0)
                {
                    woops++;
                    mp[str1[i] - 'a' + 'A']--;
                }
            }
            else if (str1[i] >= 'A' && str1[i] <= 'Z')
            {
                if (mp[str1[i] + 'a' - 'A'] > 0)
                {
                    woops++;
                    mp[str1[i] + 'a' - 'A']--;
                }
            }
        }
    }
    cout << yay << " " << woops << "\n";

}

int main()
{
    solve();
}

更短(MCVE)解决方案的版本 -

#include <cstdio>
#include <iostream>
#include <map>
#include <cstdlib>
#include <string>
using namespace std;

void solve()
{
    string str1, str2, ans;
    int mp[1000];
    for (int i = 0; i < 100;i++)
    {
        mp[i] = 0;
    }
    cin >> str1 >> str2;
    int yaya = 0, woops = 0;
    for (int i = 0; i < str2.length();i++)
    {
        mp[str2[i]]++;
    }
    for (int i = 0; i < str1.length(); i++)
    {
        if (mp[str1[i]] > 0)
        {
            yaya++;
            mp[str1[i]]--;
        }
        else
        {
            if (str1[i] >= 'a' && str1[i] <= 'z')
            {
                if (mp[str1[i] - 'a' + 'A'] > 0)
                {
                    woops++;
                    mp[str1[i] - 'a' + 'A']--;
                }
            }
            else if (str1[i] >= 'A' && str1[i] <= 'Z')
            {
                if (mp[str1[i] + 'a' - 'A'] > 0)
                {
                    woops++;
                    mp[str1[i] + 'a' - 'A']--;
                }
            }
        }
    }
    cout << yaya << " " << woops << "\n";

}

int main()
{
    solve();
}

1 个答案:

答案 0 :(得分:3)

在第一个循环中,您不会初始化所有值:

int mp[1000];
for (int i = 0; i < 100; i++) // 100 and not 1000
{
    mp[i] = 0;
}

所以以后对mp[i]的任何访问都有未定义的值。

您可以直接撰写int mp[1000] = {};mp初始化0