map, string 强大的STL

Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 50,000 words.
Only one case.
 
Output
Your output should contain all the hat’s words, one per line, in alphabetical order.
 
Sample Input
 
   
a ahat hat hatword hziee word
 
Sample Output
 
   
ahat hatword
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
# include<cstring>
using namespace std;
#define MAX 51000
map<string,int> m;
 string ch[MAX];
int main()
{
    char ch1[100],ch2[100];
    int i=0;

    while(cin>>ch[i])
    {
       m[ch[i++]]=1;
    }
    for(int j=0;j<i;j++)
    {
        for(int k=1;k<ch[j].size()-1;k++)
        {
             string ch1(ch[j],0,k);     //表示把ch[j]中从下标0開始连续的k个字符串赋给ch1  
             string ch2(ch[j],k,ch[j].size()-k);
            if(m[ch1]==1&&m[ch2]==1)
            {
               cout<<ch[j]<<endl;
                break;
            }
        }
    }
    return 0;
}


 指针做:::;
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <string>
# include<cstring>
using namespace std;
#define MAX 51000
 string ch[MAX];
struct node{
    bool isword;
    node  *next[26];
    node()
    {
        isword=0;
        memset(next,NULL,sizeof(next));
    }
};

int insert(node *root,string s)
{
    node *p=root;
    for(int i=0;i<s.size();i++)
    {
        int id=s[i]-'a';
       if(p->next[id]==NULL)  p->next[id]= new node();
           p= p->next[id];
    }
    p->isword=1;
}
int find(node *root,string s)
{
    node *p =root;
    for(int i=0;i<s.size();i++)
    {
        int id=s[i]-'a';
        if(p->next[id]==NULL) return 0;
        p=p->next[id];
    }
    return p->isword;
}
int main()
{
    char ch1[100],ch2[100];
    int i=0;
    node *root=new node();
    while(cin>>ch[i])
    {
        insert(root,ch[i]);
        i++;
    }
    for(int j=0;j<i;j++)
    {
        for(int k=1;k<ch[j].size()-1;k++)
        {
             string ch1(ch[j],0,k);     //表示把ch[j]中从下标0開始连续的k个字符串赋给ch1
             string ch2(ch[j],k,ch[j].size()-k);
            if(find(root,ch1)==1&&find(root,ch2)==1)
            {
               cout<<ch[j]<<endl;
                break;
            }
        }
    }
    return 0;
}

Codeforces Round #286 (Div. 2)    A. Mr. Kitayuta's Gift


题意:  给你一个字符串,从字符a到字符z选一个字母插入不论什么位置使之成为回文串,不行输出“NA”。


代码:    讨厌暴力有木有。

。。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

bool  ispal(string str)
{
    string hh=str;
    reverse(hh.begin(),hh.end());
    return hh==str ? 1:0;
}
int main()
{
    string ss;
    while(cin>>ss)
    {
        bool flag=0;
        int len=ss.size();
        int h,i;
        for(i=0; i<=len; i++)
        {

            for(char j='a'; j<='z'; j++)
            {
                string xa;
                for(h=0; h<i; h++) xa+=ss[h];
                xa+=j;
                for(h=i; h<len; h++)  xa+=ss[h];
                if(ispal(xa))
                {
                    cout<<xa<<endl;
                    flag=1;
                    break;
                }
            }
            if(flag) break;
        }
        if(!flag)  puts("NA");

    }
    return 0;
}

Codeforces Round #300  Cutting Banner
题意:
         在一段不超过100的字符串中减掉一些子串之后能组成"CODEFORCES"则输出YES
技巧:   对string进行取子串操作,S.substr(start,length)

int main()
{
#ifndef  ONLINE_JUDGE
    freopen("in.cpp","r",stdin);
#endif
    string s;
    string str1;

   while(cin>>s)
   {
       for(int i=0;i<s.size();i++)
       {
           for(int j=i+1;j<s.size();j++)
           {
               str1=s.substr(0,i)+s.substr(j);
                if(str1=="CODEFORCES")
                {
                    printf("YES\n");
                    return 0;
                }
           }
       }
        puts("NO");
   }
    return 0;
}*/


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值