1.
//1 //you //i love you,oyu love me //jack int main() { string s,word,tihuan; cin>>s; getchar(); getline(cin,word); cin>>tihuan; vector<string>part; int i=0; string temp; while(i<word.size()) { if(word[i]==' ' || word[i]==',') { part.push_back(temp); temp=""; if(word[i]==',') { string g=","; part.push_back(g); } } else { temp.push_back(word[i]); } i++; } part.push_back(temp); sort(s.begin(),s.end()); for(int i=0;i<part.size();i++) { string temp=part[i]; sort(temp.begin(),temp.end()); if(temp==s) part[i]=tihuan; } string result=""; for(int i=0;i<part.size();i++) if(part[i]==",") { result.pop_back(); result=result+part[i]; } else result=result+part[i]+' '; cout<<result<<endl; }
2.回溯
#include <iostream> #include <bits/stdc++.h> using namespace std; //回溯 vector<string>result; string temp=""; void backward(vector<string>part,int index) { if(temp.size()==part.size()) result.push_back(temp); else if(temp.size()<part.size()) { for(int i=0;i<part[index].size();i++) { temp.push_back(part[index][i]); backward(part,index+1); temp.pop_back(); } } } int main() { string s,cache; getline(cin,s); vector<string>part; int i=0; //切割字符 while(i<s.size()) { if(s[i]==' ') { part.push_back(cache); cache=""; } else cache.push_back(s[i]); i++; } part.push_back(cache); //回溯 for(int i=0;i<part[0].size();i++)//start { temp.push_back(part[0][i]); backward(part,1); temp.pop_back(); } //處理回路 vector<int>flag(result.size(),0); for(int i=0;i<result.size();i++) { set<int>S; for(int j=0;j<result[i].size();j++) { if(S.find(result[i][j])==S.end())//not find S.insert(result[i][j]); else { flag[i]=1; break; } } } for(int i=0;i<result.size();i++) if(flag[i]) cout<<result[i]+"--circular dependency"<<endl; else cout<<result[i]<<endl; }3,。各位大佬怎么做的啊
全部评论
(4) 回帖