POJ 3573 I18n (字符串模拟)

题意:

把无冲突的缩写根据格式变成全称

分析:

POJ,stringT,,string
这种需要根据格式输出大小写的最好先 normalize , 我是全变小写就好搞了, 功能都写成函数不易出错
煞笔了, 把题意读成了一定有空格间隔, WA,string T , 无论怎么优化, 要是知道比赛在 POJstring
曾经在 POJ 我都特么 map 都被卡过

代码:

//
//  Created by TaoSama on 2015-10-05
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

struct Word {
    char c[35];
    int l;
    bool operator< (const Word& rhs) const {
        return strcmp(c, rhs.c) < 0;
    }
    bool operator!= (const Word& rhs) const {
        if(l == rhs.l) return strcmp(c, rhs.c);
        return true;
    }
} word;

char line[85];
map<Word, Word> mp;
bool isWord;

Word abbr(Word s) {
    Word x; x.l = -1;
    if(isWord) {
        if(s.l < 4) return x;
        for(int i = 0; i < s.l; ++i) s.c[i] = tolower(s.c[i]);
        sprintf(x.c, "%c%d%c", s.c[0], s.l - 2, s.c[s.l - 1]);
        if(mp.count(x)) {
            Word &tmp = mp[x];
            if(tmp.l != -1 && tmp != s) tmp.l = -1;
        } else mp[x] = s;
        return x;
    } else {
        s.c[0] = tolower(s.c[0]);
        s.c[s.l - 1] = tolower(s.c[s.l - 1]);
//        puts(s.c);
        return mp.count(s) ? mp[s] : x;
    }
}

void format(Word& s, Word& t) {
    if(isupper(s.c[0])) {
        if(islower(s.c[s.l - 1])) {
            for(int i = 0; i < t.l; ++i) {
                if(i == 0) putchar(toupper(t.c[i]));
                else putchar(t.c[i]);
            }
        } else for(int i = 0; i < t.l; ++i) putchar(toupper(t.c[i]));
    } else {
        for(int i = 0; i < t.l; ++i) putchar(t.c[i]);
    }
}

int main() {
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(false);

    while(gets(line)) {
        for(int i = 0; line[i];) {
            if(!isalnum(line[i])) putchar(line[i++]);

            int p = 0;
            isWord = true;
            while(isalnum(line[i])) {
                if(isdigit(line[i])) isWord = false;
                word.c[p++] = line[i++];
            }
            word.c[p] = 0; word.l = p;
            Word t = abbr(word);
            if(~t.l) format(word, t);
            else printf("%s", word.c);
        }
        puts("");
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值