栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

IEEE极限编程:Word Ordering

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

IEEE极限编程:Word Ordering

马上要参加IEEE编程比赛了,找两道简单的题目练习一下。

首先贴上自己写的代码:

#include 
#include 
#include 
#include 
using namespace std;
#include 
#include 

int find_position(vector& alpha, char target) {
    int nPosition=0;
    vector ::iterator iElement = find(alpha.begin(),
        alpha.end(),target);
    if (iElement != alpha.end())
    {
       nPosition = distance(alpha.begin(), iElement);
    }
    return nPosition;
}
void compare_string(string& a, int len_a, string& b, int len_b, vector& alpha) {
    int length = len_a > len_b ? len_b:len_a;
    for (int i = 0; i < length; i++) {
        if (find_position(alpha,a[i]) > find_position(alpha,b[i])) {
            string temp = a;
            a = b;
            b = temp;
            return;
        }
        else if (find_position(alpha, a[i]) < find_position(alpha, b[i])) {
            return;
        }
    }
    if(len_a <= len_b){
        return;
    }
    else{
        string temp = a;
        a = b;
        b = temp;
        return;
    }
}

int main(int argc, char* argv[])
{
    vectoralpha;
    string alpha_str;
    cin>>alpha_str;
    string temp=alpha_str;
    for(unsigned i=0;i>N;
    for (unsigned i = 0; i < alpha_str.size(); i++) {
        alpha.push_back(alpha_str[i]);
    }
    vectorexample(N);
    for (int i = 0; i < N; i++) {
        cin>> example[i];
    }
    for (unsigned  i = 0; i < example.size(); i++)
    {
        for (unsigned  j = example.size() - 2; j >= i; j--) 
        {
            compare_string(example[j], example[j].size(),example[j + 1], example[j+1].size(),alpha);
            if(j==0) break;
        }
    }
    for (unsigned  i = 0; i < example.size(); i++) {
        cout << example[i]< 

改来改去通过率也只有百分之七十,因为用的是冒泡排序,时间复杂度较高,有百分之三十的用例是超时的。在我修改的过程中一个叫Nickel的哥们用C++提交了一版感觉挺不错的,下面是他的代码:

#include 
#include 
#include 
#include 
#include 
using namespace std;

map m;

bool compare(const string& s1, const string& s2){
    int len = s1.length() > s2.length() ? s2.length() : s1.length();
    for(int i=0; i> s;
    cin >> N;

    for(int i=0; i l(N);
    for(int i=0; i> l[i];
    }

    sort(l.begin(), l.end(), compare);
    for(string s : l){
        cout << s << endl;
    }
    return 0;
}

对比感觉自己的思维方式,编程技巧和一些常用的函数都很欠缺。
这次比赛也不指望得奖,自己还是有自知之明的,只希望能够借这个机会长长见识,养成刷题和做记录的好习惯。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/330518.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号