博客:

加速:ios::sync_with_stdio(false);

举个例子:

題目:输入的第一行有一个数字 N 代表接下來有 N 行资料,每一行资料里有不固定个数的整数(最多20个,每行最大200个字元),编程將每行的总和打印出來。

输入:

3
1 2 3
20 17 23 54 77 60
111 222 333 444 555 666 777 888 999

输出:

6
251
4995

代码:

#include <iostream>
#include <string>
#include <sstream>
using namespace std; int main()
{
    string s;
    stringstream ss;
    int n;     cin >> n;
    getline(cin, s);  //读取换行
    for (int i = ; i < n; i++)
    {
        getline(cin, s);
        ss.clear();
        ss.str(s);         int sum = ;         while ()
        {
            int a;             ss >> a;
            if(ss.fail())
                break;
            sum += a;
        }
        cout << sum << endl;
    }     return ;
}

字典树的结合

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
using namespace std;
const int max_=2e4+;
int tot;
int trie[max_][];
bool book[max_];
void insert(string str)
{
int len=str.size();
int rt=;
for(int i=;i<len;i++)
{
int k=str[i]-'a';
if(!trie[rt][k])
trie[rt][k]=++tot;
rt=trie[rt][k];
}
book[rt]=;
}
bool find_(string str)
{
int len=str.size();
int rt=;
for(int i=;i<len;i++)
{
int k=str[i]-'a';
if(!trie[rt][k])
return ;
rt=trie[rt][k];
}
if(book[rt])
return ;
else
return ;
}
int main()
{
ios::sync_with_stdio(false);
string str1,str2;
while(getline(cin,str1))
{
if(str1=="#")
break;
tot=;
int ant=;
memset(trie,,sizeof(trie));
memset(book,,sizeof(book));
stringstream ss(str1);
while(ss>>str2)
{
//cout<<ss<<endl;
if(!find_(str2))
{
ant++;
insert(str2);
}
}
printf("%d\n",ant);
}
}

最新文章

  1. ASP.NET MVC路由规则
  2. 解决Yum安装依赖问题
  3. 在 Server 端存取 Excel 檔案的利器:NPOI Library
  4. 5.7 cm server-agent 会出现无法启动
  5. 图数据库 Titan 高速入门
  6. 为Textview里面的ImageSpan添加点击响应事件
  7. Struts2动态方法调用
  8. spring boot / cloud (二十) 相同服务,发布不同版本,支撑并行的业务需求
  9. Mac ---- markdown 转 html\word\pdf
  10. COSC2309/2347 Semester 1, 2019
  11. 批量插入数据, 将DataTable里的数据批量写入数据库的方法
  12. &lt;script&gt; 属性crossorigin
  13. FIDDLER的使用方法及技巧总结(连载三)FIDDLER使用技巧及方法
  14. SpringCloud负载均衡笔记
  15. no_namespace rename 在C++中是什么意思啊
  16. 【JUC源码解析】ReentrantReadWriteLock
  17. java代码------------条件运算符 ?:
  18. sshfs的挂载与卸载
  19. BZOJ5319: [Jsoi2018]军训列队
  20. strust2的10种type类型

热门文章

  1. Cocos2d-x之引擎框架简介
  2. 第一周复习一 ( HTML表单form)
  3. 都 9102 年了,一起来攻克 Linux 吧!
  4. Python列表推导式中使用if-else
  5. 学习Spring时用Idea中Maven Project创建一个子module出现了子pom.xml没有继承父pom.xml
  6. Spring学习笔记(9)——注入参数
  7. url传送图片的base64编码给web客户端
  8. linux下如何挂载磁盘
  9. shell变量替换扩展 变量测试
  10. vue+vue-cli2+webpack配置资源cdn