Abbreviation Gym

来源:互联网 发布:淘宝店客户具体地址 编辑:程序博客网 时间:2024/06/06 01:27

题目链接: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 AbCOh  No    Extra Spaces.And,Punctuation Ruin Everything.Wa Wa Wa WaA Great Opportunity  for all contestants..Bn Fb.Bn ,. .Fb Xx.*/