谁懂在外网刷水题的快乐

freopen(".in","r",stdin);
freopen(".out","w",stdout);
#include<bits/stdc++.h>
#include<functional>
#include<windows.h>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<string>
#include<vector>
#include<cctype>
#include<ctime>
#include<map>
#include<set>
using namespace std;
string toLower(const string& s) {
string res = s;
for (auto& c : res) c = tolower(c);
return res;
}
string trim(const string& s) {
size_t start = s.find_first_not_of(" \t\r\n");
size_t end = s.find_last_not_of(" \t\r\n");
return (start == string::npos) ? "" : s.substr(start, end - start + 1);
}
vector<string> tokenize(const string& s) {
vector<string> tokens;
string word;
for (char c : s) {
if (isalnum(c) || c == '_' || (unsigned char)c > 127) {
word += c;
} else {
if (!word.empty()) { tokens.push_back(word); word.clear(); }
}
}
if (!word.empty()) tokens.push_back(word);
return tokens;
}
bool contains(const string& haystack, const string& needle) {
return haystack.find(needle) != string::npos;
}
double similarity(const string& a, const string& b) {
set<string> setA, setB, intersection;
for (auto& w : tokenize(toLower(a))) setA.insert(w);
for (auto& w : tokenize(toLower(b))) setB.insert(w);
for (auto& w : setA) if (setB.count(w)) intersection.insert(w);
if (setA.empty() && setB.empty()) return 1.0;
return (double)intersection.size() / (setA.size() + setB.size() - intersection.size());
}
string currentTime() {
time_t t = time(nullptr);
char buf[64];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&t));
return string(buf);
}
enum IntentType {
INTENT_GREETING,
INTENT_FAREWELL,
INTENT_TIME,
INTENT_WEATHER,
INTENT_JOKE,
INTENT_CALC,
INTENT_REMIND,
INTENT_NAME,
INTENT_HELP,
INTENT_MOOD,
INTENT_SEARCH,
INTENT_TRANSLATE,
INTENT_UNKNOWN,
INTENT_INSULT,
INTENT_BDSM
};
struct Entity {
string type;
string value;
};
vector<Entity> extractEntities(const string& input) {
vector<Entity> entities;
string num;
for(size_t i=0;i<input.size();i++) {
if(isdigit(input[i])||input[i]=='.') {
num += input[i];
} else {
if (!num.empty()) {
entities.push_back({"number", num});
num.clear();
}
}
}
if(!num.empty()) entities.push_back({"number",num});
for(size_t i=0;i<input.size();i++) {
if(input[i]=='"') {
size_t end=input.find('"',i+1);
if(end!=string::npos) {
entities.push_back({"quoted", input.substr(i+1,end-i-1)});
i=end;
}
}
}
return entities;
}
struct IntentRule {
IntentType intent;
vector<string> keywords;
vector<string> patterns;
double threshold;
};
class IntentRecognizer {
vector<IntentRule> rules;
public:
IntentRecognizer() {
rules.push_back({INTENT_BDSM,
{"色图","肏","性交","精子","精液","麻豆","彭海洋","BDSM","暗网","91小网站"},
{"gay","GAY","Lala","LALA","lesbian","Lesbian","男同","女同","卵巢","睾丸","前列腺"},0.3});
rules.push_back({INTENT_INSULT,
{"傻", "笨", "滚", "死", "废", "智障", "脑残", "白痴", "垃圾", "混蛋", "傻逼", "滚蛋"},
{"你是不是傻", "你个笨蛋", "滚开", "真没用","草泥马","fuck","FUCK","信球","鸡巴"}, 0.5});
rules.push_back({INTENT_GREETING,
{"你好", "hello", "hi", "嗨", "hey", "早安", "早上好", "晚上好", "下午好"},
{"你好啊", "在吗", "在不在", "哈喽"}, 0.4});
rules.push_back({INTENT_FAREWELL,
{"再见", "拜拜", "bye", "goodbye", "走了", "下次见", "回见"},
{"先走了", "我要走了"}, 0.4});
rules.push_back({INTENT_TIME,
{"时间", "几点", "日期", "今天", "星期", "什么时候"},
{"现在几点", "今天几号", "什么日子"}, 0.3});
rules.push_back({INTENT_WEATHER,
{"天气", "气温", "下雨", "晴天", "温度", "weather"},
{"今天天气", "明天天气", "天气怎么样"}, 0.3});
rules.push_back({INTENT_JOKE,
{"笑话", "搞笑", "开心一下", "逗我", "joke", "段子"},
{"讲个笑话", "来个笑话", "说个段子"}, 0.3});
rules.push_back({INTENT_CALC,
{"计算", "算一下", "等于多少", "+", "-", "*", "/"},
{"帮我算", "计算一下", "等于几"}, 0.3});
rules.push_back({INTENT_REMIND,
{"记住", "提醒", "备忘", "记录", "记一下", "别忘了"},
{"帮我记住", "帮我记一下", "提醒我"}, 0.3});
rules.push_back({INTENT_NAME,
{"名字", "叫什么", "你是谁", "介绍", "自我介绍", "who"},
{"你叫什么", "你叫什么名字"}, 0.4});
rules.push_back({INTENT_HELP,
{"帮助", "help", "能做什么", "功能", "怎么用", "指令"},
{"你能做什么", "有什么功能", "怎么用你"}, 0.3});
rules.push_back({INTENT_MOOD,
{"开心", "难过", "伤心", "生气", "无聊", "累", "烦", "happy", "sad"},
{"我好累", "我不开心", "我很伤心", "好无聊"}, 0.3});
rules.push_back({INTENT_SEARCH,
{"搜索", "查一下", "搜一下", "查找", "search", "百度", "谷歌"},
{"帮我搜", "帮我查", "搜一下"}, 0.3});
rules.push_back({INTENT_TRANSLATE,
{"翻译", "translate", "英文怎么说", "中文怎么说"},
{"帮我翻译", "翻译一下"}, 0.3});
}
IntentType recognize(const string&input) {
string lower = toLower(input);
double bestScore=0;
IntentType bestIntent=INTENT_UNKNOWN;
for(auto&rule:rules) {
double score=0;
for(auto&kw:rule.keywords) {
if(contains(lower,toLower(kw))) {
score=max(score,0.8);
}
}
for(auto&pat:rule.patterns) {
if(contains(lower, toLower(pat))) {
score=max(score,0.9);
}
}
for(auto&kw:rule.keywords) {
double sim=similarity(input, kw);
score=max(score,sim);
}
if(score>bestScore&&score>=rule.threshold) {
bestScore=score;
bestIntent=rule.intent;
}
}
return bestIntent;
}
};
struct ConversationContext {
string userName;
vector<string> memory;
vector<pair<string,string>> history;
IntentType lastIntent;
int turnCount;
};
class ResponseGenerator {
ConversationContext& ctx;
string evaluateCalc(const string& input) {
double a=0,b=0;
char op='+';
bool found=0;
for(size_t i=0;i<input.size();i++) {
if(input[i]=='+'||input[i]=='-'||input[i]=='*'||input[i]=='/') {
if(i>0&&isdigit(input[i-1])) {
op=input[i];
a=stod(input.substr(0,i));
b=stod(input.substr(i+1));
found=1;
break;
}
}
}
if(!found) return "抱歉,我暂时只能处理简单的四则运算,格式如:78+91";
double result=0;
switch(op) {
case '+':result=a+b;break;
case '-':result=a-b;break;
case '*':result=a*b;break;
case '/':
if (b == 0) return "输入不合法!!!";
result=a/b;break;
}
return "计算结果是:"+to_string(result);
}
string Bdsm() {
string bdsm="法律风险提示:传播这类涉色情内容的信息属于违法行为,2026年1月1日起施行的新修订《治安管理处罚法》明确规定,无论公开传播还是私发给好友,传播淫秽信息最高可处10-15日拘留并处5000元罚款,传播数量较大、情节严重的还会构成刑事犯罪,面临有期徒刑等处罚。同时这类内容会对未成年人的身心健康造成严重不良影响,我国多部法律法规都明确禁止制作、传播淫秽色情物品、图像、信息。";
return bdsm;
}
string handleInsult() {
vector<string> fucks={
"妈了个逼的你是不是没M!!!",
"狗嘴里吐不出象牙来!",
"你鸡鸡是不是炸了?!",
"再骂死老冯!!!",
"妈了个逼的小心我给你鸡鸡薅下来!!!",
"我是不是给你逼脸给多了?",
"你全家是不是爆浆了?!",
"我祝你出门被果粒橙操死,玩水被海洋淹死!",
"你妈了个逼的大骚鸡!!!"
};
return fucks[rand()%fucks.size()];
}
string randomJoke() {
vector<string> jokes={
"为什么程序员总是分不清万圣节和圣诞节?因为 Oct 31 == Dec 25。(八进制31 = 十进制25)",
"一个 SQL 语句走进一家酒吧,看到两张桌子(table),于是走上前问:'我可以 JOIN 你们吗?'",
"世界上最长的英文单词是什么?smiles——因为首尾字母之间隔了一英里(mile)。",
"老师问小明:'用果然造个句子。' 小明说:'先吃水果,然后喝水。'",
"为什么数学书总是很忧郁?因为它有太多问题了。",
"一只蜗牛爬上了苹果树,树上的毛毛虫问:'你来干嘛?' 蜗牛说:'苹果还没熟,我先排个队。'"
};
return jokes[rand() % jokes.size()];
}
string handleMood(const string& input) {
string lower = toLower(input);
if(contains(lower,"开心")||contains(lower,"高兴")||contains(lower,"happy"))
return "太好了!看到你开心我也很高兴!?? 有什么好事分享一下?";
if(contains(lower,"难过") || contains(lower,"伤心") || contains(lower,"sad"))
return "别难过,一切都会好起来的。要不要聊聊发生了什么事?我在这里陪你。??";
if(contains(lower,"生气") || contains(lower,"烦"))
return "深呼吸,先冷静一下。要不要跟我说说是什么事让你不开心?说出来会好受一些。";
if(contains(lower,"无聊"))
return "无聊的话,不如让我给你讲个笑话?或者我们可以玩个小游戏!";
if(contains(lower,"累"))
return "辛苦了!适当休息很重要哦。要不要我帮你放首'文字歌曲'放松一下???";
return "我感受到你的情绪了,想聊聊吗?我一直在这里。";
}
public:
ResponseGenerator(ConversationContext&c):ctx(c){}
string generate(IntentType intent,const string&input) {
vector<Entity> entities=extractEntities(input);
switch (intent) {
case INTENT_GREETING: {
string name=ctx.userName.empty()?"朋友":ctx.userName;
vector<string> resps={
"你好呀,"+name+"!今天有什么我能帮你的吗?",
"嗨,"+name+"!很高兴见到你~",
"你好!我是你的智能助手,随时为你服务!"
};
return resps[rand()%resps.size()];
}
case INTENT_FAREWELL:{
string name=ctx.userName.empty()?"朋友":ctx.userName;
vector<string> resps={
"再见,"+name+"!期待下次和你聊天~",
"拜拜!有需要随时找我哦!",
"下次见!祝你一切顺利!"
};
return resps[rand()%resps.size()];
}
case INTENT_TIME:
return "现在是 "+currentTime()+"。";
case INTENT_WEATHER:
return "抱歉,我目前无法获取实时天气数据。不过你可以告诉我你在哪个城市,我可以给你一些出行建议!";
case INTENT_JOKE:
return randomJoke();
case INTENT_CALC:
return evaluateCalc(input);
case INTENT_INSULT:
return handleInsult();
case INTENT_BDSM:
return Bdsm();
case INTENT_REMIND: {
string memo;
for(auto&e:entities) {
if(e.type=="quoted"){memo=e.value;break; }
}
if(memo.empty()) {
string cleaned=input;
vector<string> prefixes={"帮我记住", "帮我记一下", "记住", "提醒我", "备忘", "记录一下", "记一下"};
for(auto&p:prefixes) {
size_t pos=cleaned.find(p);
if(pos!=string::npos) {
cleaned=cleaned.substr(pos+p.size());
break;
}
}
memo=trim(cleaned);
}
if(!memo.empty()) {
ctx.memory.push_back(memo);
return "好的,我已经帮你记住了:\""+memo+"\"。目前共记录了 "+
to_string(ctx.memory.size())+" 条备忘。";
}
return "你想让我记住什么呢?可以用引号括起来,比如:帮我记住 \"明天下午3点开会\"";
}
case INTENT_NAME: {
string lower=toLower(input);
if(contains(lower,"我叫")||contains(lower,"我是")) {
size_t pos=input.find("叫");
if(pos!=string::npos&&pos+1<input.size()) {
ctx.userName=trim(input.substr(pos+1));
return "你好,"+ctx.userName+"!我记住你的名字了~";
}
}
return "我是 PHY智能体,一个用 C++ 编写的智能聊天引擎。我可以识别意图、提取信息、做简单计算、帮你记事等等!";
}
case INTENT_HELP:
return string(
"我能帮你做这些事:\n"
"1.打招呼/告别 —— 说'你好'或'再见'\n"
"2.查时间 —— 说'现在几点'\n"
"3.讲笑话 —— 说'讲个笑话'\n"
"4.简单计算 —— 说'计算 123+456'\n"
"5.备忘录 —— 说'帮我记住 xxx'\n"
"6.聊天解闷 —— 说说你的心情\n"
"7.搜索/翻译 —— 说'帮我搜 xxx'\n"
"8.查看备忘 —— 说'查看备忘'\n"
"9.退出 —— 说'退出'或'quit'"
"10.PHY名字的由来");
case INTENT_MOOD:
return handleMood(input);
case INTENT_SEARCH:
return "PHY: 你想搜索的内容我收到了!不过作为离线引擎,我无法直接联网。你可以把关键词告诉我,我帮你整理思路。";
case INTENT_TRANSLATE:
return "PHY:翻译功能需要接入词典库。你可以告诉我具体要翻译什么,我会尽力帮你!";
case INTENT_UNKNOWN:
default: {
if(contains(toLower(input),"查看备忘")||contains(toLower(input),"我的备忘")) {
if (ctx.memory.empty()) return "你还没有任何备忘记录。";
string res="PHY:你的备忘录:\n";
for(size_t i=0;i<ctx.memory.size();i++) {
res+=" "+to_string(i+1)+". "+ctx.memory[i]+"\n";
}
return res;
}
if(contains(toLower(input),"清除备忘")||contains(toLower(input),"删除备忘")) {
ctx.memory.clear();
return "备忘录已清空!";
}
vector<string> fallbacks={
"嗯...我不太确定你的意思,能换个说法吗?",
"这个问题有点超出我的能力范围了,你可以试试说'帮助'看看我能做什么。",
"抱歉,我没理解。你可以试试更简洁的表达,或者输入'帮助'查看我的功能。",
"我还在学习中...你可以试试问我时间、让我讲笑话、帮你做计算等。"
};
return fallbacks[rand()%fallbacks.size()];
}
}
}
};
class PHY {
IntentRecognizer recognizer;
ConversationContext context;
ResponseGenerator generator;
bool running;
public:
PHY() : generator(context), running(true) {
srand(time(nullptr));
context.turnCount=0;
context.lastIntent=INTENT_UNKNOWN;
}
void start() {
string library_st[15]={"Talk算法库","Claw算法库","DefenderX算法库","Imagine算法库",
"Joke算法库","Mood算法库","SeeTime算法库","Record算法库","Seek算法库","Orange Juice智能体"};
for(int i=0;i<10;i++) {
cout<<"PHY "<<library_st[i]<<"接入成功!\n";
Sleep(50);
}
cout<<"\n";
Sleep(500);
cout<<"系统自检完成!错误:0。警告:0。\n\n";
Sleep(100);
system("pause");
system("cls");
cout<<"========================================\n";
cout<<" PHY智能体 已启动\n";
cout<<" 输入 '帮助' 查看功能 | 输入 '退出' 结束\n";
cout<<"========================================\n\n";
string input;
while(running) {
cout<<"你: ";
getline(cin,input);
input=trim(input);
if (input.empty()) continue;
string lower = toLower(input);
if (lower=="退出"||lower=="quit"||lower=="exit"||lower=="q") {
cout<<"PHY:再见!感谢使用 PHY,期待下次见面!\n";
break;
}
IntentType intent=recognizer.recognize(input);
context.lastIntent=intent;
context.turnCount++;
string response=generator.generate(intent,input);
context.history.push_back({input,response});
cout<<"PHY:"<<response<<"\n\n";
Sleep(3000);
system("cls");
}
}
};
int main() {
PHY engine;
engine.start();
return 0;
}
//Ren Boda