PC110302/UVA10010

下周开始就省选了,ACM的日子在今年内应该就会结束了,大三了,最后一次机会了,小小感伤一下……

今天广州下大雨,心情怪怪的,感觉码不出质量高的,又很久没做过PC了,就刷刷水题吧。

老实说Program challenge的题目,输入输出特麻烦……搞到我PE了三次,从没试过……我今天的转速很低。

这题目有点像字符串匹配,规模也很小,随便写了一下。

我发现我现在做字符串很喜欢用map。c++有三宝,stl,template,class.其中我觉得最重要的就前两者啦,虽然很多用都以面向对象来形容c++,但是c++的更精华在与stl,模版。

好啦,回归正题,

题目要求按米字八个方向寻找单词,并是上左优先,一定能找到。
我的算法(准确来说是做法,没什么算法可言)。经过基本的tolow操作,化成小写字母,之后利用map,对每个出现的字母映射到一个vector,从上到下,从左到右的位置。

之后对每一个要匹配的单词,取出第一个字母,找到对应的vector,每一个point去做检索,检索到了,就是最优了,输出,OK。

由于今天心情真的不怎么样,做完SOLVED后没有看别人代码了。


/*******************************************************************************/
/* OS           : 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 UTC 2013 GNU/Linux
 * Compiler     : g++ (GCC)  4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
 * Encoding     : UTF8
 * Date         : 2014-03-30
 * All Rights Reserved by yaolong.
*****************************************************************************/
/* Description: ***************************************************************
*****************************************************************************/
/* Analysis: ******************************************************************
*****************************************************************************/
/*****************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<map>
using namespace std;
class point
{
public:
    int x,y;
    point(int x,int y)
    {
        this->x=x;
        this->y=y;
    }
    point() {}

};
char toLow(char a)
{
    if(a<'a') return a+'a'-'A';
    return a;

}
map<char,vector<point>  > mp;
vector<string> str;
char p[60][60];
int dx[]= {1,1, 1,-1,-1,-1,0, 0};
int dy[]= {1,0,-1, 1, 0,-1,1,-1};
void fun(string s)
{
    int siz=s.size();
    int i=0,j;
    char cur_s;
    int cur_i=1;
    point pt=mp[s[0]][i];
    while(cur_i<siz)
    {
        pt=mp[s[0]][i++];

        for(j=0; j<8; j++)
        {
           int nx=pt.x,ny=pt.y;
            int flag=1;
            cur_i=1;

            while(cur_i<siz)
            {
        
                cur_s=s[cur_i];
                nx+=dx[j];
                ny+=dy[j];
                if(p[nx][ny]!=cur_s)
                {
                    flag=0;
                    break;
                }

                    cur_i++;



            }
            if(flag) break;

        }

    }
    cout<<pt.x<<" "<<pt.y<<endl;

}
int main()
{

    char tmp;
    int cases,n,m,i,j,k;
    int first=1;
 
    cin>>cases;

        while(cases--)
        {
           if(first) first=0;
        else cout<<endl;
            mp.clear();
            str.clear();
            getchar();
            cin >>n>>m;
            memset(p,'.',sizeof(p));
            for(i=1; i<=n; i++)
                for(j=1; j<=m; j++)
                {
                    cin>>tmp;
                    tmp=toLow(tmp);
                    mp[tmp].push_back(point(i,j));
                    p[i][j]=tmp;

                }
            cin>>k;
            str.resize(k);
            for(i=0; i<k; i++)
            {
                cin>>str[i];
                for(j=0; j<str[i].length(); j++)
                    str[i][j]=toLow(str[i][j]);
            }
            for(i=0; i<k; i++)
            {

                fun(str[i]);


            }



        }





    return 0;

}





原文地址:https://www.cnblogs.com/dengyaolong/p/3697209.html