zoukankan      html  css  js  c++  java
  • 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 = 0; i < n; i++)
        {
            getline(cin, s);
            ss.clear();
            ss.str(s);
     
            int sum = 0;
     
            while (1)
            {
                int a;
     
                ss >> a;
                if(ss.fail())
                    break;
                sum += a;
            }
            cout << sum << endl;
        }
     
        return 0;
    }
    View Code

    字典树的结合

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<iostream>
    #include<string.h>
    #include<sstream>
    using namespace std;
    const int max_=2e4+5;
    int tot;
    int trie[max_][26];
    bool book[max_];
    void insert(string str)
    {
        int len=str.size();
        int rt=0;
        for(int i=0;i<len;i++)
        {
            int k=str[i]-'a';
            if(!trie[rt][k])
                trie[rt][k]=++tot;
            rt=trie[rt][k];
        }
        book[rt]=1;
    }
    bool find_(string str)
    {
        int len=str.size();
        int rt=0;
        for(int i=0;i<len;i++)
        {
            int k=str[i]-'a';
            if(!trie[rt][k])
                return 0;
            rt=trie[rt][k];
        }
       if(book[rt])
        return 1;
       else
        return 0;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        string str1,str2;
        while(getline(cin,str1))
        {
            if(str1=="#")
                break;
            tot=0;
            int ant=0;
            memset(trie,0,sizeof(trie));
            memset(book,0,sizeof(book));
            stringstream ss(str1);
            while(ss>>str2)
            {
                //cout<<ss<<endl;
                if(!find_(str2))
                {
                    ant++;
                    insert(str2);
                }
            }
            printf("%d
    ",ant);
        }
    }
    View Code
  • 相关阅读:
    vue使用百度地图
    Genymotion模拟器使用camera
    angular集成tinymce
    react-native环境搭建
    linux系统下安装ssl证书(tomcat)
    vue图片上传及java存储图片(亲测可用)
    那些年vue踩过的坑
    垃圾分类装置仿真实训
    《报任安书》文言文化常识闯关游戏·网络版
    《报任安书》文言文化常识闯关游戏
  • 原文地址:https://www.cnblogs.com/linhaitai/p/9942733.html
Copyright © 2011-2022 走看看