字符串处理 | 北邮OJ | 103. 反转单词

https://vpn.bupt.edu.cn/http/10.105.242.80/problem/p/103/

对于这种题要引起重视了

  • 傻逼代码
#include <bits/stdc++.h>
#define FF(a,b) for(int a=0;a<b;a++)
#define F(a,b) for(int a=1;a<=b;a++)
#define LEN 100
#define INF 1000000
#define bug(x) cout<<#x<<"="<<x<<endl;

using namespace std;
typedef long long ll;
const double pi=acos(-1);


char buf[1010];

int main()
{
//    freopen("./in","r",stdin);
    char line[1010];
    vector<string> v;

    while(scanf("%s",buf)){
        v.push_back(string(buf));
        memset(buf,0,sizeof buf);	//没有就会用上次的数据,贼傻逼
        char c=getchar();
        if(c=='\n' ||c==EOF ){
            reverse(v.begin(),v.end());
            FF(i,v.size()){
                printf("%s",v[i].c_str());
                if(i!=v.size()-1)
                    printf(" ");
            }
            puts("");
            v.clear();
            if( c==EOF)
                break;
        }

    }
    return 0;
}
  • cin的现代化版本
#include <bits/stdc++.h>
#define FF(a,b) for(int a=0;a<b;a++)
#define F(a,b) for(int a=1;a<=b;a++)
#define LEN 100
#define INF 1000000
#define bug(x) cout<<#x<<"="<<x<<endl;

using namespace std;
typedef long long ll;
const double pi=acos(-1);


char buf[1010];

int main()
{
//    freopen("./in","r",stdin);
    ios::sync_with_stdio(false);//这东西开了,stdio的东西都别想用了
    string s;
    vector<string> v;
    while(cin>>s){
        v.clear();
        v.push_back(s);
        while(1){
//            char c=getchar();
            char c=cin.get();//老老实实用这个,上一行读出来的都是EOF
            if(c=='\n' || c==EOF)
                break;
            cin>>s;
            v.push_back(s);
        }
        reverse(v.begin(),v.end());
        FF(i,v.size()){
            cout<<v[i];
            if(i!=v.size()-1)
                cout<<' ';
        }
        cout<<endl;
    }
    return 0;
}

在这里插入图片描述
数据量很小,差异不大

  • 读一行做split的信息化版本
#include <bits/stdc++.h>
#define FF(a,b) for(int a=0;a<b;a++)
#define F(a,b) for(int a=1;a<=b;a++)
#define LEN 100
#define INF 1000000
#define bug(x) cout<<#x<<"="<<x<<endl;

using namespace std;
typedef long long ll;
const double pi=acos(-1);

void split(string s,char splitchar,vector<string>& vec)
{
    int L = s.length();
    int start=0;
    string topush;
    for(int i=0; i<L; i++)
    {
        if(s[i] == splitchar && i == 0)//第一个就遇到分割符
        {
            start += 1;
        }
        else if(s[i] == splitchar)
        {
            topush=s.substr(start,i - start);
            if(topush.length()>0)
                vec.push_back(topush);
            start = i+1;
        }
        else if(i == L-1)//到达尾部
        {
            topush=s.substr(start,i+1 - start);
            if(topush.length()>0)
                vec.push_back(topush);
        }
    }
}


int main()
{
    freopen("./in","r",stdin);
    string s;
    vector<string> v;
    char line[1010];
    while(gets(line)){
        vector<string> v;
        split(string(line),' ',v);
        reverse(v.begin(),v.end());
        FF(i,v.size()){
            printf("%s",v[i].c_str());
            if(i!=v.size()-1)
                printf(" ");
        }
        puts("");
    }
    return 0;
}

C++真是个垃圾语言,连split都没有

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值