zoukankan      html  css  js  c++  java
  • 7.25习题

    Email address in Berland is a string of the form A@B, where A and B are arbitrary strings consisting of small Latin letters.

    Bob is a system administrator in «Bersoft» company. He keeps a list of email addresses of the company's staff. This list is as a large string, where all addresses are written in arbitrary order, separated by commas. The same address can be written more than once.

    Suddenly, because of unknown reasons, all commas in Bob's list disappeared. Now Bob has a string, where all addresses are written one after another without any separators, and there is impossible to determine, where the boundaries between addresses are. Unfortunately, on the same day his chief asked him to bring the initial list of addresses. Now Bob wants to disjoin addresses in some valid way. Help him to do that.

    Input

    The first line contains the list of addresses without separators. The length of this string is between 1 and 200, inclusive. The string consists only from small Latin letters and characters «@».

    Output

    If there is no list of the valid (according to the Berland rules) email addresses such that after removing all commas it coincides with the given string, output No solution. In the other case, output the list. The same address can be written in this list more than once. If there are several solutions, output any of them.

    Examples

    Input
    a@aa@a
    Output
    a@a,a@a
    Input
    a@a@a
    Output
    No solution

    这道题不算难,但也不是很简单
    首先要完全理解题意,@前后的字符不一定只有一个,可能是这样a@ac,条件要考虑完整;
    其次,输出问题,统计@的个数n,那么就有
    n-1个" ,"
    #include<iostream>
    using namespace std;
    string s;
    int main()
    {
        int cnt=0;
        int ans=0;
        cin>>s;
        int k=s.length();
        for(int i=0;i<k;i++)
            {
                if(s[i]=='@')
                  cnt++;
            }
        if(s[0]=='@'||k<3||cnt<1||s[k-1]=='@')
        {
            cout<<"No solution";
            return 0;
        }
        else
        {
            cnt--;
            int flag=0;
            for(int i=1;i<s.length();i++)
            {
                if(s[i]=='@')
                {
                    if(s[i+1]!='@'&&s[i+2]!='@')
                       continue;
                    else
                    {
                        flag=1;
                        cout<<"No solution";
                        break;
                    }  
                }
            }
            if(flag==0)
            {
                for(int i=0;i<k;i++)
                {
                    if(s[i]!='@')
                       cout<<s[i];
                    else
                    {
                        cout<<s[i]<<s[i+1];
                        if(ans!=cnt)
                          cout<<",";
                        i++;
                        ans++;
                    }
                }
            }
        }
        return 0;
    }

    另一种做法就是对于@周围的字符串,再开一个数组,为了避免a@a@a这种情况的发生,需要对用过的字符进行标记,也要考虑全部都是字符没有@的情况

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
        char str[210];
        int ss[210];
        cin>>str;
        int len=strlen(str);
        memset(ss,0,sizeof(ss));
        if(str[0]=='@'||str[len-1]=='@') cout<<"No solution"<<endl;
        else{
            int flag=0;
            int cnt=0;//用来标记有@ 
            for(int i=1;i<len-1;i++){
                if(str[i]=='@'&&((ss[i-1]!=0||ss[i+1]!=0)||(str[i-1]=='@'||str[i+1]=='@'))){
                    flag=1;
                    cnt=1;
                    break;
                }
                else if(str[i]=='@'){
                    ss[i-1]=1;
                    ss[i+1]=1;
                    cnt=1;
                }
            }
            if(flag) cout<<"No solution"<<endl;
            else{
                if(cnt==0){
                    cout<<"No solution"<<endl;
                }
                else{
                    int i=0;
                while(str[i]!='@'){
                    cout<<str[i];
                    i++;
                }
                cout<<"@";
                i++;
                for(;i<len;i++){
                    if(str[i+1]!='@') cout<<str[i];
                    else cout<<","<<str[i];
                }
                }
                
            }
        }
        return 0;
    }

    我一开始没有完整的做出了,也是看了小伙伴的又重新做了,加油

  • 相关阅读:
    0129 System类 Math类 Arrays类 大数据运算
    0127 基本类型包装类
    'telnet' 不是内部或外部命令,也不是可运行的程序 解决方案
    删除时报org.springframework.dao.DataIntegrityViolationException
    mapper自动识别驼峰配置 spring MVC
    spring Security如何debug源码
    公司tomcat项目启动
    java.util.ConcurrentModificationException: null 异常解决
    @Transactional 学习
    mangoDB初探
  • 原文地址:https://www.cnblogs.com/ylrwj/p/11246861.html
Copyright © 2011-2022 走看看