1 条题解
-
0
解题思路
- 输入处理:
- 读取名字数量
g
和愿望数量w
。 - 读取
g
个名字存入name
数组。 - 读取每个愿望字符串
wish
。
- 读取名字数量
- 字符串处理函数
solve
:- 初始化
hasname
数组为false
,用于标记名字是否被包含。 - 初始化
praise
为false
表示是否包含赞美词,muchword
为false
(代码中未使用),specified
用于存储包含的名字,wordcount
用于记录单词数量,temp
用于临时存储单词。 - 遍历愿望字符串
wish
:- 如果遇到分隔符(空格或感叹号)且
temp
不为空:- 将
temp
转换为小写(若首字母为大写)。 - 检查
temp
是否是名字,若是则标记hasname
数组。 - 检查
temp
是否是赞美词,若是则设置praise
为true
。 - 清空
temp
,wordcount
加1
。
- 将
- 如果不是分隔符,则将字符添加到
temp
中。
- 如果遇到分隔符(空格或感叹号)且
- 处理最后一个单词(若
temp
不为空),进行与上述类似的操作。 - 将包含的名字存入
specified
数组。 - 根据
specified
数组的大小输出名字(若有)。 - 根据
wordcount
和praise
的值输出不同的后缀("oh"、"xixi"、"hehe")。
- 初始化
- 主函数
main
:- 读取输入的
g
和w
。 - 读取名字列表。
- 读取每个愿望字符串并调用
solve
函数进行处理。 - 使用
system("pause")
暂停程序(通常用于 Windows 系统,保持控制台窗口打开)。
- 读取输入的
#include<iostream> #include<vector> #include<string> #include<cstring> using namespace std; string name[6],dict[3] = {"pretty", "beautiful", "lovely"}; int g,w; bool hasname[6]; bool issep(char c){ return c==' '||c=='!'; } void solve(string &wish){ memset(hasname,0,sizeof(hasname)); bool praise = 0; vector<string> specified; int wordcount = 0; string temp; for(unsigned long i = 0; i < wish.size(); i++) if(issep(wish[i])&&temp.size()>0){ if(temp[0]<'a') temp[0]+='a'-'A'; for(int j = 0; j < g; j++) if(temp==name[j]){ hasname[j] = 1; break; } if(praise==0){ for(int j = 0; j < 3; j++) if(temp==dict[j]){ praise = 1; break; } } temp.clear(); wordcount++; } else if(0==issep(wish[i])) temp.push_back(wish[i]); if(temp.size()>0){ if(temp[0]<'a') temp[0]+='a'-'A'; for(int j = 0; j < g; j++) if(temp==name[j]){ hasname[j] = 1; break; } if(praise==0){ for(int j = 0; j < 3; j++) if(temp==dict[j]){ praise = 1; break; } } temp.clear(); wordcount++; } for(int i = 0; i < g; i++) if(hasname[i]) specified.push_back(name[i]); if(specified.size()>0){ cout << specified[0]; for(unsigned int i = 1; i < specified.size(); i++) cout << ' '<< specified[i]; } else cout << "All"; cout << ':'; if(wordcount<=9) cout << " oh" << endl; else if(praise) cout << " xixi" << endl; else cout << " hehe" << endl; } int main(){ cin >> g >> w; for(int i = 0; i < g; i++) cin >> name[i]; string wish; cin.get(); for(int i = 0; i < w; i++){ getline(cin,wish); solve(wish); } }
- 输入处理:
- 1
信息
- ID
- 678
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 5
- 已通过
- 2
- 上传者