日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hdu4096(模拟)

發(fā)布時間:2025/3/15 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu4096(模拟) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題意:

給出一些句子,代表一些名詞和動詞的關系,然后有一些查詢,看查詢語句中的關系是否正確。


思路:

按照題意建邊,查詢時找兩個點有沒有路徑即可。


代碼:

#include <cstdio> #include <sstream> #include <iostream> #include <cstring> #include <algorithm> #include <vector> #include <map> using namespace std;map<string,int> mm; map<string,int> nn; int tot; struct E {int to, next; }e[400000]; int head[1000]; int to;void init() {tot = 1;to = 1;mm.clear();nn.clear();memset(head, 0, sizeof(head)); }void add(int u,int v) { // printf("lalalal %d %d\n",u,v);e[to].to = v;e[to].next = head[u];head[u] = to++; }int vis[1000]; int dfs(int u,int v) {vis[u] = 1;if(u == v) return true;for(int i = head[u]; i; i = e[i].next) {int vv = e[i].to;if(!vis[vv]) {if(dfs(vv,v)) {return 1;}}}return 0; }int main() {int t;scanf("%d", &t);getchar();int kase = 1;while (t--) {init();string rans="";printf("Case #%d:\n", kase++);string sentence;while(getline(cin, sentence)){if (sentence == "") {continue;} int senlen = sentence.length();if (sentence[senlen - 1] == '!') {break;} else if (sentence[senlen - 1] == '.') {vector<string> word;stringstream ss(sentence);string nowstring;while (ss >> nowstring) {word.push_back(nowstring);} int wordslen = word.size();word[wordslen - 1] = word[wordslen - 1].substr(0, word[wordslen - 1].length() - 1); if (word[1] == "are") {if(mm[word[0]]==0)mm[word[0]]=tot++;if(mm[word[2]]==0)mm[word[2]]=tot++;add(mm[word[0]],mm[word[2]]);} if (word[1] == "can") {if(mm[word[0]]==0)mm[word[0]]=tot++;if(nn[word[2]]==0)nn[word[2]]=tot++;add(mm[word[0]],nn[word[2]]);} if (word[1] == "which") {if (word[4] == "can") {if(nn[word[3]]==0)nn[word[3]]=tot++;if(nn[word[5]]==0)nn[word[5]]=tot++; // cout<<word[3]<<" "<<word[5]<<endl;add(nn[word[3]],nn[word[5]]);}else if (word[4] == "are") {if(nn[word[3]]==0)nn[word[3]]=tot++;if(mm[word[5]]==0)mm[word[5]]=tot++;add(nn[word[3]],mm[word[5]]);}} }else if (sentence[senlen - 1] == '?') {memset(vis,0,sizeof(vis));int ans;vector<string> word;stringstream ss(sentence);string nowstring;while (ss >> nowstring) {word.push_back(nowstring);} int wordslen = word.size();word[wordslen - 1] = word[wordslen - 1].substr(0, word[wordslen - 1].length() - 1);if (word[0] == "are") {if (word.size() == 3) {if(mm[word[1]]==0)mm[word[1]]=tot++;if(mm[word[2]]==0)mm[word[2]]=tot++;int u=mm[word[1]];int v=mm[word[2]];ans=dfs(u,v);// printf("u=%d v=%d\n",u,v);}else {if(nn[word[4]]==0)nn[word[4]]=tot++;if(mm[word[5]]==0)mm[word[5]]=tot++;int u=nn[word[4]];int v=mm[word[5]];ans=dfs(u,v);// printf("u=%d v=%d\n",u,v);}}if (word[0] == "can") {if (word.size() == 6) {if(nn[word[4]]==0)nn[word[4]]=tot++;if(nn[word[5]]==0)nn[word[5]]=tot++;int u=nn[word[4]];int v=nn[word[5]];ans=dfs(u,v);// printf("u=%d v=%d\n",u,v);}else {if(mm[word[1]]==0)mm[word[1]]=tot++;if(nn[word[2]]==0)nn[word[2]]=tot++;int u=mm[word[1]];int v=nn[word[2]];ans=dfs(u,v);// printf("u=%d v=%d\n",u,v);}}if(ans)rans+="Y";else rans+="M";}}cout<<rans<<endl;}return 0; }

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的hdu4096(模拟)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。