1011: Longest word
http://acm.cug.edu.cn/JudgeOnline/problem.php?id=1011
#include<stdio.h> #include<string> #include<vector> #include<string.h> #include<algorithm> #include<iostream> using namespace std; vector<string>v; vector<string>::iterator it; int main() { string s,si; char sj[300]; while(cin.getline(sj,300)) { v.clear(); s.clear(); s=sj; memset(sj,0,sizeof(sj)); s+=' '; int max=0; for(int i=0;i<s.size();i++) { if(s[i]!=' ')si+=s[i]; else if(si.size()!=0&&si[0]<'0'||si[0]>'9') { if(si.size()>max)max=si.size(); v.push_back(si); si.clear(); } else si.clear(); } int js=0; for(it=v.begin();it!=v.end();it++) { if((*it).size()==max) { js++; if(js==1)cout<<*it<<" : "<<(*it).size(); else cout<<", "<<*it<<" : "<<(*it).size(); } } cout<<endl; } return 0; }
vector的结构体始终CE,过不了??
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string> #include<vector> #include<string.h> #include<algorithm> #include<iostream> using namespace std; struct node { int t; string ss; }x; vector<node>v; vector<node>::iterator it; bool cmp(node a,node b) { if(a.ss.size()>b.ss.size())return true; if(a.t<b.t)return true; return false; } int main() { string s,si; char sj[300]; while(cin.getline(sj,300)) { v.clear(); s.clear(); s=sj; memset(sj,0,sizeof(sj)); int js=0; s+=' '; for(int i=0;i<s.size();i++) { if(s[i]!=' ')si+=s[i]; else if(si.size()!=0&&si[0]<'0'||si[0]>'9') { x.ss=si; x.t=(++js); v.push_back(x); si.clear(); x.ss.clear(); } else si.clear(); } sort(v.begin(),v.end(),cmp); /*cout<<(*v.begin()).ss<<" : "<<(*v.begin()).ss.size(); it=v.begin(); while((it+1)!=v.end()&&(*it).ss.size()==(*(++it)).ss.size()) { cout<<", "<<(*it).ss<<" : "<<(*it).ss.size(); }*/ //js=0; cout<<(*v.begin()).ss<<" : "<<(*v.begin()).ss.size(); for(it=++v.begin();it!=v.end();it++) { if((*it).ss.size()==(*v.begin()).ss.size()) cout<<", "<<(*it).ss<<" : "<<(*it).ss.size(); } cout<<endl; } return 0; }