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