天梯赛-L1-064 估值一亿的AI核心代码 (20 分)--2019全国CCCC天梯赛L1题解

本文原创首发CSDN,链接 https://blog.csdn.net/qq_41464123/article/details/88926928 ,作者博客https://blog.csdn.net/qq_41464123 ,转载请带上本段文字,尤其是脚本之家、码神岛等平台,谢谢配合。


昨天参加了CCCC天梯赛,被L1的第八题坑到了,还有L2的第二题根本看不懂,L3更别说了,先把L1的题目补上,L2尽快补。

L1的题目主要就是第八题,其他的应该简单的把。

首先L1-8的题目是这样说的

 

  • 1.消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
  • 2.把原文中所有大写英文字母变成小写,除了 I
  • 3.把原文中所有独立的 can youcould you 对应地换成 I canI could—— 这里“独立”是指被空格或标点符号分隔开的单词;
  • 4.把原文中所有独立的 Ime 换成 you
  • 5.把原文中所有的问号 ? 换成惊叹号 !

首先我提出几点疑意:

1.要求三的“原文”二字 指的是第二步基础之上的原文还是题目刚给出的原文

换句话说  输入如果是   CaN YoU   你该输出 I can  还是输出 can you ?(因为原文不满足第三个条件)

这样看来题目具有二义性,也可能是我个人理解问题

2.根据比赛现场实践,我发现第三个要求的“原文”的意思是在第二步基础之上的原文

比如输入输入如果是   CaN YoU   你必须输出 I can

你如果原样输出 那第3 5个例子会WA

3.那么问题就来了,如果第三点要求的“原文”意思是第二步基础上的原文,那第四个要求的“原文”是否指的是第三步基础上的原文呢?  不! 实践证明  第四个要求的“原文”是第二步操作后的原文,而不是第三步后的原文!!!

举个例子 如果按照题意顺序下来,那么如果输入can you,根据第三个要求变成 I can,再跟据第四个要求变成 you can

不,不是这样的,AC答案应该输出I can

4.样例数据其中第四个的输入三个空格,正确输出是AI:加一个空格,也就是除了每个样例都要输出的“AI: ”以外,只需要输出一个回车。

5.样例第二个  考点是标点符号中间有多个空格的情况   3分

比如输入aaa,     ,   aaaa

输出应该是aaa,,aaaa中间没有空格


本文原创首发CSDN,链接 https://blog.csdn.net/qq_41464123/article/details/88926928 ,作者博客https://blog.csdn.net/qq_41464123 ,转载请带上本段文字,尤其是脚本之家、码神岛等平台,谢谢配合。


最后的我的AC思路:(保证可以AC,不一定是最优解)

1.特判  如果都是空格的情况 直接输出回车 结束 -----第四个例子

2.先把所有的字符除了I转化成小写,顺带把   ?改成  !

3.把字符串的中间空格最多留一个,(PS:先不管标点符号前的空格)

4.把需要替换的替换掉  如me 、 I 、 can you 、could you

5.去掉首尾 的空格

6.输出的时候注意如果当前是空格,后面是标点 ,则不输出该空格,否则正常输出。

 

最后代码如下:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
int d(char x)
{
	if (x >= 'A'&&x <= 'Z') return -1;
	if (x >= 'a'&&x <= 'z') return -2;
	if (x >= '0'&&x <= '9') return -3;
	if (x == ' ') return 2;
	return 1;
}
string low(string str)
{
	string b;
	b += " ";
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] >= 'A'&&str[i] <= 'Z'&&str[i] != 'I')
		{
			str[i] += 32;
		}
		if (str[i] == '?') str[i] = '!';
	}
	b += str;
	b += " ";
	str.clear();
	str += " ";
	for (int i = 1; i < b.size() - 1; i++)
	{
		if (b[i] == ' ' && b[i - 1] == ' ')	continue;
		else if (d(b[i + 1]) == 1 && b[i] == ' ')	continue;
		else	str += b[i];
	}
	str += " ";
	return str;
}
int main()
{
	int t, i, j, k;
	string str;
	cin >> t;
	getchar();
	while (t--)
	{
		getline(cin, str);
		cout << str << endl;
		cout << "AI: ";
		bool kong = false;
		for (i = 0; i < str.size(); i++)
		{
			if (str[i] != ' ')
			{
				kong = true;
				break;
			}
		}
		if (kong == false)
		{
			cout << endl;
			continue;
		}
		str = low(str);
		string temp;
		for (i = 0; i < str.size(); i++)
		{
			temp += str[i];
			if (d(str[i])>0 && str[i + 1] == 'm'&&str[i + 2] == 'e'&&d(str[i + 3])>0)
			{
				i += 2;
				temp += "you";
			}
			else if (d(str[i])>0 && str[i + 1] == 'I'&&d(str[i + 2])>0)
			{
				i++;
				temp += "you";
			}
			else if (d(str[i])>0 && str[i + 1] == 'c'&& str[i + 2] == 'a'&& str[i + 3] == 'n'&& str[i + 5] == 'y'&& str[i + 6] == 'o'&& str[i + 7] == 'u'&&d(str[i + 8])>0)
			{
				i += 7;
				temp += "I can";
			}
			else if (d(str[i])>0 && str[i + 1] == 'c'&& str[i + 2] == 'o'&& str[i + 3] == 'u'&& str[i + 4] == 'l'&& str[i + 5] == 'd'&&  str[i + 7] == 'y'&& str[i + 8] == 'o'&& str[i + 9] == 'u'&&d(str[i + 10])>0)
			{
				i += 9;
				temp += "I could";
			}
		}
		str = "";
		str += temp;
		//cout << str << endl;
		int len;
		for (i = str.size() - 1; i >= 0; i--)
		{
			if (str[i] != ' ')
			{
				len = i;
				break;
			}
		}
		int cnt = 0;
		for (i = 0; i <= len; i++)
		{
			if (i>0 && str[i] == ' '&& d(str[i + 1]) == 1)	continue;
			if (str[i] != ' ')  cout << str[i], cnt++;
			else if (cnt>0) cout << " ";
		}
		cout << endl;
	}
	return 0;
}

 

posted @ 2019-03-31 12:56  郑为中  阅读(233)  评论(0编辑  收藏  举报