OJ 生成一个由各首字母组成的缩写词

描述
给定一个由若干英文单词组成的字符串,生成一个由各首字母组成的缩写词(acronym),其中的the,a,an,of,for及and被忽略。

输入
输入数据有若干行,每行上有一个字符串对应一种情形。字符串中分隔单词的字符有空格或连字符。字符串的长度不超过65536字符。

输出
对于每一种情形,先输出“Case #:”(#为序号,从1起),然后输出结果,换行。

样例输入1
United States of America
PEOPLE’S REPUBLIC OF CHINA
The Asia-Pacific Economic Cooperation
Organization of the Petroleum Exporting Countries
United Nation Educational Scientific and Culture Organization
Universal Serial Bus

样例输出1
Case 1: USA
Case 2: PRC
Case 3: APEC
Case 4: OPEC
Case 5: UNESCO
Case 6: USB

AC代码:

#include 
#include 
#include 
using namespace std;

const int N = 65536;
string str1[N], temp;
char str2[N];

int main()
{
    int i = 0;
    while(getline(cin, temp))
    {
        i ++;       //情形序号递增

        int j, k, l, n = 0;
        stringstream ss;
        ss.str(temp);
        while(ss >> str1[n ++]);
        n --;       //输入的单词个数
        
        for(j = 0, k = 0; j <= n - 1; j ++)
        {
            if(str1[j].length() == 1)
                if(str1[j][0] == 'a' || str1[j][0] == 'A')
                    continue;   //忽略"a"
            if(str1[j].length() == 2)
            {
                if((str1[j][0] == 'a' || str1[j][0] == 'A') && (str1[j][1] == 'n' || str1[j][1] == 'N'))
                    continue;   //忽略"an"
                if((str1[j][0] == 'o' || str1[j][0] == 'O') || (str1[j][1] == 'f' || str1[j][1] == 'F'))
                    continue;   //忽略"of"
            }
            if(str1[j].length() == 3)
            {
                if((str1[j][0] == 'a' || str1[j][0] == 'A') && (str1[j][1] == 'n' || str1[j][1] == 'N') && (str1[j][2] == 'd' || str1[j][2] == 'D'))
                    continue;   //忽略"and"
                if((str1[j][0] == 't' || str1[j][0] == 'T') && (str1[j][1] == 'h' || str1[j][1] == 'H') && (str1[j][2] == 'e' || str1[j][2] == 'E'))
                    continue;   //忽略"the"
                if((str1[j][0] == 'f' || str1[j][0] == 'F') && (str1[j][1] == 'o' || str1[j][1] == 'O') && (str1[j][2] == 'r' || str1[j][2] == 'R'))
                    continue;   //忽略"for"
            }
            str2[k++] = str1[j][0]; //将每个单词首字母提取出来
            for(l = 0; l <= str1[j].length(); l ++)
                if(str1[j][l] == '-')
                    str2[k++] = str1[j][l+1];   //将连字符'-'后的首字母提取出来
        }
        cout << "Case " << i << ": " << str2 << endl;
        memset(str2, '\0', N);  //一种情形结束初始化str2
    }
    return 0;
}

你可能感兴趣的:(SHUOJ,C++,OJ)