zoukankan      html  css  js  c++  java
  • HDU 2072 单词数

    题目链接  http://acm.hdu.edu.cn/showproblem.php?pid=2072

    普通解法:

    /* HDU 2072 单词数 --- 字符串处理 */
    #include <cstdio> //C语言改成stdio.h即可
    #include <cstring> //C语言改成string.h即可
    
    const int maxn = 85;
    
    int main()
    {
        char *head1, *head2;
        char a[maxn];
        char b[maxn][maxn];
        int i, k, len, cnt1, cnt2;
    
        while (gets(a)){
            //遇到字符串"#'则结束
            if ('#' == a[0]){
                break;
            }
    
            //处理末尾的连续空格
            len = strlen(a);
            for (i = len - 1; i >= 0; --i){
                if (' ' == a[i]){
                    a[i] = 0; //末尾有空格则用空字符替换
                }
                else
                    break;
            }//for(i)
    
            //处理开头的连续空格
            len = strlen(a);
            if (len == 0){ //全是空格的情况特殊处理
                printf("0
    ");
                continue;
            }
            for (i = 0; i < len; ++i){
                if (' ' == a[i]){
                    a[i] = 0;
                }
                else{
                    //遇到非空格
                    head1 = a + i; //记录新的开头
                    break;
                }
            }//for(i)
            
            len = strlen(head1);
            //在字符串末尾补上一个空格以便处理最后一个单词
            head1[len] = ' ';
            head1[len + 1] = 0; 
            
            head2 = head1;
            cnt1 = 0; //cnt1统计单词数(可能重复)
            for (i = 0; i <= len; ++i){
                if (' ' == head1[i]){
                    head1[i] = 0;
                    //当前点是空格而后一个点不是空格 则后一个点即为新的单词起点
                    if (' ' != head1[i + 1]){
                        //记录上一个单词并更新单词数
                        strcpy(b[cnt1], head2);
                        ++cnt1;
                        head2 = head1 + i + 1; //更新新的单词起点
                    }
                }
            }//for(i)
    
            //处理相同的单词数
            cnt2 = cnt1;
            for (i = 0; i < cnt1; ++i){
                if (b[i][0] != '#'){
                    for (k = i + 1; k < cnt1; ++k){
                        if (b[k][0] != '#' && strcmp(b[i], b[k]) == 0){
                            --cnt2;
                            b[k][0] = '#'; //已经判断是否重复则不再判断
                        }
                    }//for(k)
                    b[i][0] = '#'; //到此 和b[i]相同的单词第一个字符已经全部设置成'#'
                }//if
            }//for(i)
            printf("%d
    ", cnt2);
        }//while(gets)
    
        return 0;
    }
    View Code

    STL+stringstream解法:

    /* HDU 2072 单词数 --- stringstream+STL */
    #include <cstdio>
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <set>
    using namespace std;
    
    set<string> k;
    
    int main()
    {
        string s;
        while (getline(cin, s) && s != "#"){
            k.clear();
            stringstream str(s);
            string tmp;
            while (str >> tmp){
                k.insert(tmp);
            }
            cout << k.size() << endl;
        }//while(get)
    }
    View Code
  • 相关阅读:
    解决html中刷新页面后checkbox还选中的问题
    初始化spring容器的几种方法
    在web.xml中配置spring配置文件的路径
    查找算法
    排序算法
    ORACLE TO_CHAR,TO_DATE函数格式说明
    ORACLE TO_DATE函数
    ORACLE SUBSTR函数
    ORACLE学习笔记
    Linux 查看端口占用情况
  • 原文地址:https://www.cnblogs.com/tommychok/p/5043647.html
Copyright © 2011-2022 走看看