zoukankan      html  css  js  c++  java
  • UCF Local Programming Contest 2015(Practice)D题

    这道题只需要暴力做即可,注意的是题目有可能给的是大的串包含小的串,比如lo loro这种。因此我们要优先考虑匹配大的

    这题我刚开始用了错误的写法,从头遍历给的字符串,如果有匹配就匹配,这是不对的,因为比如这种情况 lo loropo 一旦给的串是loropo ,这样如果先匹配了lo,就没法找到答案

    所以可以先用map映射后,将字符串的长度存下来,倒着匹配,如果可以匹配,则先匹配大的

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<map>
    using namespace std;
    typedef long long ll;
    const int N=1e6+10;
    const int mod=1e9+7;
    map<string,int> m1;
    vector<int> num;
    int main(){
        int n;
        int m;
        int cnt=1;
        while(cin>>n){
            int i;
            if(n==0)
                break;
            string s[51];
            for(i=1;i<=n;i++){
                cin>>s[i];
                num.push_back((int)s[i].size());
                m1[s[i]]=1;
            }
            sort(num.begin(),num.end());
            num.erase(unique(num.begin(),num.end()),num.end());
            cin>>m;
            printf("Data set #%d:
    ",cnt++);
            while(m--){
    
                string tmp;
                cin>>tmp;
                string res[501];
                int tot=0;
                int num1=0;
                int j;
                for(i=0;i<(int)tmp.size();){
                    for(j=(int)num.size()-1;j>=0;j--){
                        if((int)tmp.size()<num[j]+i)
                        continue;
                        string ss=tmp.substr(i,num[j]);
                        if(m1[ss]){
                            res[tot++]=ss;
                            num1+=num[j];
                            i=i+num[j]-1;
                            break;
                        }
                    }
                    i++;
                }cout<<"     "<<tmp<<" --- ";
                if(num1==(int)tmp.size()){
                    if(tot==1){
                        cout<<"the word is in dictionary"<<endl;
                    }
                    else{
                        cout<<"the word is concatenation of"<<endl;
                        for(i=0;i<tot;i++){
                            cout<<"          "<<res[i]<<endl;
                        }
                    }
                }
                else{
                    cout<<"misspelled word"<<endl;
                }
            }
        }
    }
    View Code
  • 相关阅读:
    设计模式复习【1】- 设计原则
    Java8的学习笔记
    Gson关于抽象类的序列化与反序列化
    关于一个Java web与JFrame的深度结合
    《重构》笔记
    JAVA8 Stream API总结的好的文章 —— 持续更
    Spring Boot引入Thymeleaf前端框架的诸多问题
    敏捷开发:原则,模式与实践——第8章 单一职责原则SRP
    maven 你应该懂得那些事
    redis之单机和主从环境搭建
  • 原文地址:https://www.cnblogs.com/ctyakwf/p/12756450.html
Copyright © 2011-2022 走看看