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
  • 相关阅读:
    Android_SQLite标准版
    AutoMapper
    ext.net实例
    Winform Ribbon开发
    数据仓库建设之《元数据管理》
    数据仓库建设之维度建模
    数据仓库建设之总方案
    一点感想
    详细探秘Linux 和 Window 双系统访问Windows 磁盘需要输入密码问题解决过程分析
    如何把数据放到C#的心里之 DB2实例
  • 原文地址:https://www.cnblogs.com/ctyakwf/p/12756450.html
Copyright © 2011-2022 走看看