string中的stringstream
加速: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);
}
}
string中的stringstream的更多相关文章
- C++中使用stringstream进行类型转换操作
stringstream包括istringstream和ostringstream,提供读写string的功能,使用时需包含stream文件.4个操作:1. stringstream strm; 创建 ...
- Java的String中的subString()方法
方法如下: public String substring(int beginIndex, int endIndex) 第一个int为开始的索引,对应String数字中的开始位置, 第二个是截止的索引 ...
- Here String 中不该进行分词
我们知道,在 Shell 中,一个变量在被展开后,如果它没有被双引号包围起来,那么它展开后的值还会进行一次分词(word splitting,或者叫拆词,分词这个术语已经被搜索引擎相关技术占用了)操作 ...
- C++string中有关字符串内容修改和替换的函数浅析
1.assign() 原型: //string (1) basic_string& assign (const basic_string& str); //substring (2) ...
- 从源代码的角度聊聊java中StringBuffer、StringBuilder、String中的字符串拼接
长久以来,我们被教导字符串的连接最好用StringBuffer.StringBuilder,但是我们却不知道这两者之间的区别.跟字符串相关的一些方法中总是有CharSequence.StringBuf ...
- Java-J2SE学习笔记-查找一个String中,subString的出现次数
1.查找一个String中,subString的出现次数 2.代码 package Test; public class TestStringContain { public static void ...
- string中常用的函数
string中常用的函数 发现在string在处理这符串是很好用,就找了一篇文章放在这里了.. 用 string来代替char * 数组,使用sort排序算法来排序,用unique 函数来去重1.De ...
- String 中的秘密
Navigation: 数据类型相关 > Delphi 的字符及字符串 > [3] - String 中的秘密 //String 的指针地址及实际的内存地址 var str: str ...
- String中的==与Empty
1.String中的==与Equals方法执行结果一样吗? 我们都知道对于引用类型"=="比较的是引用而不是具体的值,但c#中有一种神奇的叫做操作符重载的东西.官方对String类 ...
随机推荐
- python学习笔记:数据类型——数字、字符串、元祖、字典
计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各种数值.但是,计算机能处理的远不止数值,还可以处理文本.图形.音频.视频.网页等各种各样的数据,不同的数据,需要定义不同的数 ...
- sql合并字段
<!-- 对发送方式合并查询 --> <!--查询所有满足条件的营销活动 --> <select id="CRM-MK-ACT-DEFINE-SELECT&qu ...
- Spring import配置文件使用占位符
import使用占位符 连接池切换导入配置的代码: <import resource="classpath:META-INF/spring/spring-${db.connection ...
- MySQL-技术专区-数据库权限管理
前言 学习mysql数据库,对于它的权限的管理是关键的一环.所以,下面介绍的是MySQL权限的管理. MySQL权限表 MySQL数据库实际上是通过将用户写入mysql库中对应的权限表来控制访问权限的 ...
- LeetCode Array Easy 118. Pascal's Triangle
Description Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. I ...
- Apache Shiro反序列化远程代码执行
一.漏洞利用 wget https://raw.githubusercontent.com/sv3nbeast/ShiroScan/master/moule/ysoserial.jar 反弹shell ...
- Apache Hadoop集群离线安装部署(二)——Spark-2.1.0 on Yarn安装
Apache Hadoop集群离线安装部署(一)——Hadoop(HDFS.YARN.MR)安装:http://www.cnblogs.com/pojishou/p/6366542.html Apac ...
- HDU 6534 莫队+ 树状数组
题意及思路:https://blog.csdn.net/tianyizhicheng/article/details/90369491 代码: #include <bits/stdc++.h&g ...
- nodejs模块——fs模块 WriteFile写入文件
WriteFile写入文件 使用fs.writeFile(filename,data,[options],callback)写入内容到文件. 参数说明: filename String 文件名 dat ...
- 注册Bean
<spring源码深度解析>笔记 1.给容器中注册组件 (1).包扫描+组件注解: (2).@Bean(导入第三方包里面的组件): (3).@Import给容器中快速的导入一个组件: 2. ...