poj1598 测试数据通过但是wrong answer 求高手指点 5C

1598 poj

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

struct Excuse{
    string s;
    int n;
};
bool com(Excuse a,Excuse b){
    return a.n > b.n;
}

int main(){
    ifstream cin("aaa.txt");
    vector<string> vk;
    vector<Excuse>vs;
    int n, m;
    string sk;
    string s,ss;
    int knum;
    Excuse e;
    int line = 1;
    while (cin >> n >> m){
        vs.clear();
        vk.clear();
        //if (line != 1)cout << endl;

        for (int  i= 0; i < n; i++){
            cin >> sk;
            vk.push_back(sk);
        }
        getline(cin, ss);
        for (int j = 0; j < m; j++){
            getline(cin, ss);
            s = ss;
            //大写字母变小写
            for (int k = 0; k < s.size(); k++){
                if (s[k] >= 'A'&&s[k] <= 'Z')s[k] = s[k] + 32;          
            }
            knum = 0;
            //扫描关键子
            for (int i = 0; i < vk.size(); i++){
                int kn = vk[i].size();
                for (int p = 0; p < s.size() - kn +1 ; p++){
                    //截取一个子串
                    string st = "";
                    for (int k = p; k < p + kn; k++){
                        st = st + s[k];
                    }

                    //关键字在句首,且关键字后一字符不是字母;
                    if (p == 0 && st == vk[i] && isalpha(s[p + kn])){
                        knum++;
                    }
                    //关键字在居中,且前后字符不是字母
                    else if (st == vk[i]
                        && !isalpha(s[p + kn])
                        && !isalpha(s[p - 1]))knum++;                   
                }               

            }           
            e.s = ss;
            e.n = knum;
            vs.push_back(e);            
        }
        sort(vs.begin(), vs.end(), com);
        cout << "Excute Set #" << line << endl;
        line++;
        for (int i = 0; i < vs.size(); i++){
            if (i != 0 &&vs[i].n < vs[i - 1].n)break;
            //else if (i != 0 && vs[i].s == vs[i - 1].s)continue;   
            cout << vs[i].s << endl;
        }
        cout << endl;
    }

    system("pause");
}


2个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问