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;
    }

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

  • 相关阅读:
    UI Shader:平面特效火
    !!! 注意区分 lua table 变量的【原地修改】与【重新赋值】
    四舍五入 :先加 0.5, 再向下取整
    预乘α
    【 pivot 】 用代码动态设置 pivot 会导致UI 立即发生位移!
    【DrawCall】 unity 动态合批被打断的规律总结
    Text 首行缩进
    SVN 不小心上传,想要回撤提交,同时还要想保存本地的更改
    unity中打开文件夹选择文件返回文件的路径
    复制文件到指定目录
  • 原文地址:https://www.cnblogs.com/ylrwj/p/11246861.html
Copyright © 2011-2022 走看看