Online Judge HDU - 1073 (字符串)

https://vjudge.net/problem/HDU-1073

这里不介绍题目了,长驱直入 直奔主题:

我们暂时定义需要核对的字符为"有效字符", 其他的为"空白符"

1.一定要认真读题,题目中The data will at most 5000 characters. 首先START 和 END 之间的所有都是 data,然后为什么强调字符数不强调行数呢?自己思考后发现,太##妙了!仅用一维字符数组就可以解决这个问题
2.即使这样,空行的处理也是必要的,不然SAMPLE第三组会凉凉,那我们用两个变量分别记录 标准输出 和 USER’S 的行数 这样AC条件就多了一个
3.题目可没说 data 一定是由数字和字母组成的,我们应该把全集定为字符集那么用 isspace 区分空白符和有效字符就好啦
4.需要getchar()换行符的地方不要忘了,START & END 都是不做对比处理的
5.重点在于PE和WA:只需判断s串里每个有效字符是否在r串里按顺序出现,不能多不能少不能乱序 & r串也必须符合此条件 成立PE,反之WA

当然,实现方法很多,博主第一次C++提交30ms左右,第二次微改G++提交15ms,第三次换了判断方法G++ 0ms
详见下码 :

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include<iomanip>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
const int maxn = 1e4+10;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
int dz[] = {0, 0, 0, 0, 1,-1};
int dx[] = {1,-1, 0, 0, 0, 0};
int dy[] = {0, 0, 1,-1, 0, 0};
struct P {
    int z, x, y;
    P(int z = 0, int x = 0, int y = 0): z(z), x(x), y(y) {}
};
//int h, r, c, t;
//int s[55][55][55];
//int d[55][55][55];
//inline bool in_border (int mz, int mx, int my) {
//    return mx>=0 && mx<r && my>=0 && my<c && mz>=0 && mz<h;
//}
bool cmp (const void* p1, const void* p2) {
    return true;
}

int main() {int T; cin >> T; getchar(); //换行符
    string t, s, r;
    while(T--) {
        s = r = "";
        int hs = 0, hr = 0; // 非常重要 用来判断行数

        getline(cin, t); // "START"
        while(getline(cin, t) && t != "END") {s += t; hs++;}
        getline(cin, t); // "START"
        while(getline(cin, t) && t != "END") {r += t; hr++;}
        if(hs == hr && s == r) cout << "Accepted" << endl; //双条件

        else {
            bool FP = true; // 假 -> WA  真 -> PE
            int i = 0, u = 0;
            for(; i < s.length(); i++) {
                if(isspace(s[i])) continue;
                bool fp = false; //先假设无匹配有效字符
                for(; u < r.length(); u++) {
                    if(s[i]==r[u]) {
                        fp = true; u++; break; //找到记得要 u++
                    }
                    if(!isspace(r[u])) break; //顺序不能换的 否则判“WA”
                }

                if(!fp) {FP = false; break;}
            }
            if(FP)
                for(; u < r.length(); u++) // 把剩余查完(若有)
                    if(!isspace(r[u])) {FP = false; break;} //大意了 有次把 u 写成 i

            if(FP) cout << "Presentation Error" << endl;
            else cout << "Wrong Answer" << endl;
        }
    }
    return 0;
}
发布了54 篇原创文章 · 获赞 43 · 访问量 1959

猜你喜欢

转载自blog.csdn.net/Jungle_st/article/details/104643680