Abbreviation Gym - 101190A(模拟)

题目链接:https://vjudge.net/problem/Gym-101190A

题意:恶心的模拟题,写了两个多小时,最后少写了一行代码就一行!!!!结果一直错,最近真是太浮躁了这样低级的错误都会犯,弄得我一下午心情都很郁闷。题意就是规定一类单词:首字母大写,之后有一个或多个小写字母。如果这类单词之间只由一个空格间隔,则可以用每个单词的大写字母组成这些字母的缩写,输出缩写+空格+(+原来的这串字符串+),其他不符合要求的单词和间隔原样输出。单词只会由大写字母或小写字母组成,间隔可以由空格逗号和点号组成。

思路:模拟题还能有啥思路,照着题意些就好了嘛,是我自己脑残写挫了哭 

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<sstream>
#include<deque>
#include<stack>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const int  maxn = 150 + 20;
const int  maxt = 300 + 10;
const int mod = 10;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int Dis[] = {-1, 1, -5, 5};
const double inf = 0x3f3f3f3f;
const int MOD = 1000;
const double PI = acos(-1.0);
int n, m, k;
vector<string> word;
vector<string> jiange;
string ans;
bool is_word[maxn];
bool is_space[maxn];
bool judge1(string s){//判断单词是否是大写字母+若干小写字母形式
    int len = s.size();
    if(len == 1) return false;//A
    bool ok2 = true;
    if(s[0] >= 'a' && s[0] <= 'z') return false;
    for(int i = 1; i < len; ++i){
        if(!(s[i] >= 'a' && s[i] <= 'z')) return false;
    }
    return true;
}
bool judge2(string s){//判断间隔是否是一个空格
    if(s.size() != 1) return false;
    if(s[0] != ' ') return false;
    return true;
}
void init(){//初始化
    ans = "";
    word.clear();
    jiange.clear();
    memset(is_word, true, sizeof is_word);
    memset(is_space, true, sizeof is_space);
}
void add_word(string tmp, int flag){//处理连续可进行缩写的单词
    string suoxie = "";
    int lentmp = tmp.size();
    for(int i = 0; i < lentmp; ++i){
        if(tmp[i] >= 'A' && tmp[i] <= 'Z'){//得到大写字母组成的缩写
            suoxie += tmp[i];
        }
    }
    if(flag == 2){//Abc Bcs abs
        tmp[lentmp - 1] = ')';
        suoxie += ' ';
        ans = ans + suoxie + "(" + tmp;
    }
    else{
        suoxie += ' ';
        ans = ans + suoxie + "(" + tmp + ")";
    }
}
int main(){
    freopen("abbreviation.in","r",stdin);
    freopen("abbreviation.out","w",stdout);
    string ss,s, s2, tmp;
    while(getline(cin, ss)){
        init();
        ss.push_back('.');//最后可能没有间隔,加一个.就不能用分类讨论了(1)
        int len = ss.size();
        int x;
        ans = "";
        for(x = 0; x < len; ++x){//将首个字母前面出现的间隔先存到ans中(2)
            if(isalpha(ss[x]))  break;
                ans += ss[x];
        }
        //经过(1)(2)操作后可以保证每个单词后面有一个间隔,下面判断每个单词和每个间隔是否符合要求
        s = "";
        int cnt = 0;
        for(int i = x; i < len; ++i){
            s.push_back(ss[i]);
        }
        s2 = s;
        len = s.size();//T_T
        for(int i = 0; i < len; ++i){
            if(s2[i] == '.' || s2[i] == ',') s2[i] = ' ';
        }
        stringstream input(s2);
        while(input >> tmp){//得到所有的单词,存到word中
            word.push_back(tmp);
        }
        int lenw = word.size();
        for(int i = 0; i < lenw; ++i){//判断每个单词是否符合要求
            is_word[i] = judge1(word[i]);
        }
        tmp = "";
        bool ok = false;
        for(int i = 0; i < len; ++i){//得到所有的间隔,存到jiange中
            if(s[i] == ' ' || s[i] == '.' || s[i] == ','){
                ok = true;
                tmp += s[i];
            }
            if(ok && isalpha(s[i])){
                jiange.push_back(tmp);
                ok = false;
                tmp = "";
            }
        }
        if(!isalpha(s[len - 1])){//最后一个间隔也存进去(因为前面在这一行字符串最后添加了一个'.',最后面一定有一个间隔)
            jiange.push_back(tmp);
        }
        int lenj = jiange.size();
        for(int i = 0; i < lenj; ++i){//判断每个间隔是否只是一个空格
            is_space[i] = judge2(jiange[i]);
        }
        tmp = "";
        int num = 0;
        int lentmp;
        for(int i = 0; i < lenw; ++i){//从头扫每个单词和间隔,分情况添加到ans中,最后输出结果ans
            if(is_word[i] && is_space[i]){
                tmp += word[i];
                tmp += jiange[i];
                ++num;
            }
            else if(is_word[i] && !is_space[i]){
                if(num >= 1){
                    tmp += word[i];
                    add_word(tmp, 1);//Abc Bcs.
                    ans += jiange[i];
                }
                else{//num == 0  Bcs. Bas Asd,
                    ans = ans + tmp + word[i] + jiange[i];
                }
                tmp = "";
                num = 0;
            }
            else if(!is_word[i]){
                if(num >= 2){
                    add_word(tmp, 2);//Abc Bcs abs
                    ans += " ";
                }
                else{
                    ans += tmp;//Bcs abs
                }
                ans = ans + word[i];
                ans += jiange[i];
                tmp = "";
                num = 0;
            }
        }
        len = ans.size();
        for(int i = 0; i < len - 1; ++i){
            printf("%c", ans[i]);
        }
        printf("\n");
    }
    return 0;
}




/*
This is ACM North Eastern European Regional Contest,
sponsored by International Business Machines.
The. Best. Contest. Ever.
A Great Opportunity for all contestants.
ab Ab A Abc AB Abcd ABc Abcde AbC
Oh  No    Extra Spaces.And,Punctuation Ruin Everything.
Wa Wa Wa Wa
A Great Opportunity  for all contestants.
.Bn Fb
.Bn ,. .Fb Xx.


*/


`date --help`命令用于显示Linux系统上`date`命令的帮助信息和可用选项。以下是`date --help`命令的输出: ``` Usage: date [OPTION]... [+FORMAT] or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] Display the current time in the given FORMAT, or set the system date. Formatting: %a locale's abbreviated weekday name (e.g., Sun) %A locale's full weekday name (e.g., Sunday) %b locale's abbreviated month name (e.g., Jan) %B locale's full month name (e.g., January) %c locale's date and time (e.g., Thu Mar 3 23:05:25 2005) %C century; like %Y, except omit last two digits (e.g., 20) %d day of month (e.g., 01) %D date; same as %m/%d/%y %e day of month, space padded; same as %_d %F full date; same as %Y-%m-%d %g last two digits of year of ISO week number (see %G) %G year of ISO week number (see %V); normally useful only with %V %h same as %b %H hour (00..23) %I hour (01..12) %j day of year (001..366) %k hour, space padded ( 0..23); same as %_H %l hour, space padded ( 1..12); same as %_I %m month (01..12) %M minute (00..59) %n a newline %N nanoseconds (000000000..999999999) %p locale's equivalent of either AM or PM; blank if not known %P like %p, but lower case %r locale's 12-hour clock time (e.g., 11:11:04 PM) %R 24-hour hour and minute; same as %H:%M %s seconds since 1970-01-01 00:00:00 UTC %S second (00..60) %t a tab %T time; same as %H:%M:%S %u day of week (1..7); 1 is Monday %U week number of year, with Sunday as first day of week (00..53) %V ISO week number, with Monday as first day of week (01..53) %w day of week (0..6); 0 is Sunday %W week number of year, with Monday as first day of week (00..53) %x locale's date representation (e.g., 12/31/99) %X locale's time representation (e.g., 23:13:48) %y last two digits of year (00..99) %Y year %z +hhmm numeric timezone (e.g., -0400) %:z +hh:mm numeric timezone (e.g., -04:00) %::z +hh:mm:ss numeric time zone (e.g., -04:00:00) %Z alphabetic time zone abbreviation (e.g., EDT) By default, date pads numeric fields with zeroes. The following optional flags may follow '%': - (hyphen) do not pad the field _ (underscore) pad with spaces 0 (zero) pad with zeros ^ use upper case if possible # use opposite case if possible After any flags comes an optional field width, as a decimal number; then an optional modifier, which is either E to use the locale's alternate representations if available, or O to use the locale's alternate numeric symbols if available. ``` 该命令列出了`date`命令支持的所有选项和格式化选项,可以帮助您更好地了解如何使用该命令。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值