1011: Longest word

本文介绍了使用C++处理字符串的方法,包括从输入中提取非数字字符串并按长度排序的过程。通过对字符串进行逐字符分析,文章实现了找出最长字符串并按长度进行排序的功能。

http://acm.cug.edu.cn/JudgeOnline/problem.php?id=1011

#include<stdio.h>
#include<string>
#include<vector>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
vector<string>v;
vector<string>::iterator it;
int main()
{
    string s,si;
    char sj[300];
    while(cin.getline(sj,300))
    {
        v.clear();
        s.clear();
        s=sj;
        memset(sj,0,sizeof(sj));
        s+=' ';
        int max=0;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]!=' ')si+=s[i];
            else if(si.size()!=0&&si[0]<'0'||si[0]>'9')
            {
                if(si.size()>max)max=si.size();
                v.push_back(si);
                si.clear();
            }
            else si.clear();
        }
        int js=0;
        for(it=v.begin();it!=v.end();it++)
        {
            if((*it).size()==max)
            {
                js++;
                if(js==1)cout<<*it<<" : "<<(*it).size();
                else cout<<", "<<*it<<" : "<<(*it).size();
            }

        }
        cout<<endl;
    }
    return 0;
}

vector的结构体始终CE,过不了??

View Code
#include<stdio.h>
#include<string>
#include<vector>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
struct node
{
    int t;
    string ss;
}x;
vector<node>v;
vector<node>::iterator it;
bool cmp(node a,node b)
{
    if(a.ss.size()>b.ss.size())return true;
    if(a.t<b.t)return true;
    return false;
}
int main()
{
    string s,si;
    char sj[300];
    while(cin.getline(sj,300))
    {
        v.clear();
        s.clear();
        s=sj;
        memset(sj,0,sizeof(sj));
        int js=0;
        s+=' ';
        for(int i=0;i<s.size();i++)
        {
            if(s[i]!=' ')si+=s[i];
            else if(si.size()!=0&&si[0]<'0'||si[0]>'9')
            {
                x.ss=si;
                x.t=(++js);
                v.push_back(x);
                si.clear();
                x.ss.clear();
            }
            else si.clear();
        }
        sort(v.begin(),v.end(),cmp);

        /*cout<<(*v.begin()).ss<<" : "<<(*v.begin()).ss.size();
        it=v.begin();
        while((it+1)!=v.end()&&(*it).ss.size()==(*(++it)).ss.size())
        {
            cout<<", "<<(*it).ss<<" : "<<(*it).ss.size();
        }*/
        //js=0;
        cout<<(*v.begin()).ss<<" : "<<(*v.begin()).ss.size();
        for(it=++v.begin();it!=v.end();it++)
        {
            if((*it).ss.size()==(*v.begin()).ss.size())
            cout<<", "<<(*it).ss<<" : "<<(*it).ss.size();
        }
        cout<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/XDJjy/archive/2013/05/03/3056720.html

返回的错误为Trying: longest_word(['cat','dog','bird']) Expecting: 'bird' ********************************************************************** File "e:\Learn AI-Assisted Python Programming,Second Edition translation\code6.3.py", line 7, in __main__.longest_word Failed example: longest_word(['cat','dog','bird']) Expected: 'bird' Got: 'bird' Trying: longest_word(['happy','birthday','my','cat']) Expecting: 'birthday' ********************************************************************** File "e:\Learn AI-Assisted Python Programming,Second Edition translation\code6.3.py", line 9, in __main__.longest_word Failed example: longest_word(['happy','birthday','my','cat']) Expected: 'birthday' Got: 'birthday' Trying: longest_word(['happy']) Expecting: 'happy' ********************************************************************** File "e:\Learn AI-Assisted Python Programming,Second Edition translation\code6.3.py", line 11, in __main__.longest_word Failed example: longest_word(['happy']) Expected: 'happy' Got: 'happy' Trying: longest_word(['cat','dog','me']) Expecting: 'cat' ********************************************************************** File "e:\Learn AI-Assisted Python Programming,Second Edition translation\code6.3.py", line 13, in __main__.longest_word Failed example: longest_word(['cat','dog','me']) Expected: 'cat' Got: 'cat' Trying: longest_word(['','']) Expecting: '' ********************************************************************** File "e:\Learn AI-Assisted Python Programming,Second Edition translation\code6.3.py", line 15, in __main__.longest_word Failed example: longest_word(['','']) Expected: '' Got: '' 1 items had no tests: __main__ ********************************************************************** 1 items had failures: 5 of 5 in __main__.longest_word 5 tests in 2 items. 0 passed and 5 failed. ***Test Failed*** 5 failures.
08-17
Trying: longest_word(['cat', 'dog', 'bird']) Expecting: 'bird' ********************************************************************** File "e:\Learn AI-Assisted Python Programming,Second Edition translation\code6.3.py", line 7, in __main__.longest_word Failed example: longest_word(['cat', 'dog', 'bird']) Expected: 'bird' Got: 'bird' Trying: longest_word(['happy', 'birthday', 'my', 'cat']) Expecting: 'birthday' ********************************************************************** File "e:\Learn AI-Assisted Python Programming,Second Edition translation\code6.3.py", line 9, in __main__.longest_word Failed example: longest_word(['happy', 'birthday', 'my', 'cat']) Expected: 'birthday' Got: 'birthday' Trying: longest_word(['happy']) Expecting: 'happy' ********************************************************************** File "e:\Learn AI-Assisted Python Programming,Second Edition translation\code6.3.py", line 11, in __main__.longest_word Failed example: longest_word(['happy']) Expected: 'happy' Got: 'happy' Trying: longest_word(['cat', 'dog', 'me']) Expecting: 'cat' ********************************************************************** File "e:\Learn AI-Assisted Python Programming,Second Edition translation\code6.3.py", line 13, in __main__.longest_word Failed example: longest_word(['cat', 'dog', 'me']) Expected: 'cat' Got: 'cat' Trying: longest_word(['', '']) Expecting: '' ********************************************************************** File "e:\Learn AI-Assisted Python Programming,Second Edition translation\code6.3.py", line 15, in __main__.longest_word Failed example: longest_word(['', '']) Expected: '' 1 items had no tests: __main__ ********************************************************************** 1 items had failures: 5 of 5 in __main__.longest_word 5 tests in 2 items. 0 passed and 5 failed. ***Test Failed*** 5 failures.这是代码输出结果
08-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值