PTA 7-18 新浪微博热门话题

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hao_zong_yin/article/details/78336494

新浪微博可以在发言中嵌入“话题”,即将发言中的话题文字写在一对“#”之间,就可以生成话题链接,点击链接可以看到有多少人在跟自己讨论相同或者相似的话题。新浪微博还会随时更新热门话题列表,并将最热门的话题放在醒目的位置推荐大家关注。

本题目要求实现一个简化的热门话题推荐功能,从大量英文(因为中文分词处理比较麻烦)微博中解析出话题,找出被最多条微博提到的话题。


MAP瞎暴

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
map<string, int> vis;
map<string, int> cnt;
map<string, int>::iterator it;
bool judge(char c) {
    if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || '0' <= c && c <= '9' || c == ' ') return true;
    return false;
}
int main() {
    int n; scanf("%d", &n); getchar();
    char str[1000];
    string s;
    for (int i = 1; i <= n; i++) {
        gets(str);
        bool flag = false;
        vis.clear();
        for (int i = 0; str[i]; i++) {
            if (str[i] == '#' && flag == false) {
                s.clear(); flag = true;
            }
            else if (str[i] != '#' && flag == true) {
                if (!judge(str[i])) str[i] = ' ';
                if ('A' <= str[i] && str[i] <= 'Z') str[i] += 32;
                int len = s.size();
                if (len > 0 && str[i] == ' ' && s[len - 1] == ' ') continue;
                s.push_back(str[i]);
            }
            else if (str[i] == '#' && flag == true) {
                string ss;
                if (s[0] == ' ') for (int i = 1; i < s.size(); i++) ss.push_back(s[i]);
                else if (s[s.size() - 1] == ' ') for (int i = 0; i < s.size() - 1; i++) ss.push_back(s[i]);
                else ss = s;
                //cout << ss << endl;
                if (!vis[ss]) {
                    vis[ss] = 1;
                    cnt[ss]++;
                }
                flag = false;
            }
        }
    }
    int num = 0;
    int res = 0;
    string ans;
    for (it = cnt.begin(); it != cnt.end(); it++) {
        string x = it->first; int y = it->second;
        if (y > res) {
            num = 0;
            res = y;
            ans = x;
        }
        else if (y == res) num++;
    }
    ans[0] -= 32;
    cout << ans << endl << res << endl;
    if (num) cout << "And " << num << " more ..." << endl;
}


展开阅读全文

VC++热门话题

08-18

程序的要求是:你看能不能帮我设计这样一道程序:校园导游咨询 题目:编制一个为来访客人进行最短路径导游的程序. 分析:1:选取校园景点,抽象成一个无向带权图。意图的顶点表示景点,边上的权值表示两地之间的距离。 2:目的:为用户提供路径咨询。根据用户指定的始点和终点输出相应路径。 rn可以先用c来调试,最好是转化成C++语言,我的E-MAIL:sprinkle153@sohu.comrnqq:112103386,请各位高手帮忙,调好后请发回我的邮箱,谢谢!!!rn头文件:rn#includern#define maxvtxnum 20rntypedef structrnstring name;rnstring info;rnvertextype;rntypedef structrnint length;rnint ivex,jvex;rnedgetype;rntypedef struct edgenodernedgetype elem;rnedgenode *ilink,*jlink;rnedgnode,*edgeptr;rntypedef structrnvertextype data;rnedgeptr firstedge;rnvnode;rntypedef structrnvnode adjmulist[maxvtxnum];rnint vexnum,edgenum;rngraphtype;rnvoid initialization();rnvoid redcommand(char &cmd);rnvoid interpret(char cmd);rnvoid initgrah(graphtype & g);rnstatus locatevex(graphtype& g,string uname,int & i);rnvoid getvex(graphtype g,int i,vertextype& v);rnedgeptr firstedge(graghtype g,int vi);rnvoid nexedge(graphtype g,int vi,edgptr p,int &vj,edgeptr &q);rnvoid insertvex(graphtype &g,vertextype v);rnvoid insertedge(graphtype& g,edgetype e);rnvoid deletevex(graphtype& g,vertextype v);rnvoid deleteedge(graphtype& g,egdetype e);typedef structrnint vx,vy;rnedge;rntypedef structEdhe edges[masvttxnum];rnint len;rnpathtype;rntypedef structstring vertices [maxutxnum];rnint num;ptype;rnvoid initpath(pathtype &pa);rnvoid copypath(pathtype&p1,pathtype p2);rnvoid insertpath(pathtype &pa,int u,int w);rnint pathlength(pathtype pa);rnvoid creatgraph(graphtype &g,file *f);rnvoid outpath(grapthtype g,pathtype pa,ptype &vtxes);rn程序:void initiatization()rnrnclrscr;rnscanf(filename);rnfin=fopen(filename,'r');rncreatgraph(g,fin);rnrnvoid creatgraph(graphtype &g,file *f)rninitgraph(g);rnfscanf(f,g.vexnum,g.edgenum);rnfor(i=0;ielem=e;rnp->ilink=firstedge(g,e.ivex);rnp->jlink=firtedge(g,e.jvex);rng.adjmulist[e.ivex].firstedge=g.adjmulist[e.jvex].firstedge=p;rnrnvoid readcommand(char& cmd)rnrndorncmd=getchel;rnwhile(cmd 不属于['s','S','v','V','q','Q']);rnrnvoid interpret(char cmd)swith (cmd)rncase's','S':rnscanf(sname);rnprintscenery(sname);rnbreak;rncase 'v','V':rnscanf(sanme ,tname);rngetshortestpath(ga,sname,tname,pathlen,spath);rnprintpath(spath,pathlen);rnbreak;rncase'q','Q':rnrnvoid printsceneryrnvoid getshortespath(graphtype g,string sname,string tname;int &pathlengh,ptype &pathinfo)rnrnlocatevex(g,sname,sv);rnlocatevex(g.tname,tv);rnshortestpath(g,sv,tv,pathlength pathinfo);rnrnvoid shortestpath(graphtype g,int st,int nd,int & pathlength,ptype & pathinfo)rnrnfor(i=0;ilength;rninsertpath(path[adjvex],st,adjvex);rnp=q;rnrnfound=FALSE;rninitset(ss);rnputinset(st,ss);rnwhile(!fount)rnmin=minval(dist);rnif(min==nd)fount=true;rnelsernv=min;putinset(v,ss);p=firstedge(g,v);rnwhile(p)rnnextedge(g,p,v,q,w);rnif(!inset(w,ss)&&(dist[v]+p->length)length;rncopypath(path[w],path[v]);rninsertpath(path[w],v,w);rnp=q;rnrnrnpathlength=dis[nd];rnoutpath(g,path[nd],pathval);rnrnstatus locatevex(graphtype &g,string uname,int &i)rnvoid initpath(pathtype &pa)pa.len=0;rnvoid insertpath(pathtype&pa ,int u,int w)rnpa.edges[pa.len].vx=u;rnpa.edges[pa.len].vy=w;rnpa.len++;rnrnvoid copypath(pathtype &p1,pathtype p2)rnrnfor(i=0;ielem.ivex==vi)q=p->ilink;vj=p->elem.jvex)rnelseq=p->jlink;vj=p->elem.ivex;rnrnrnrnvoid main()rnrninitialization;rndornreadcommand(cmd);rninterpret(cmd);rnwhile(cmd!='q'&&cmd!='Q');rnrn 论坛

没有更多推荐了,返回首页