题意:

  给出一个模式串pattern,再给出一个串str,问str的模板是否是pattern。

思路:

  注意点:只要对于所有pattern[i]相同的i,str中对应的所有words[i]也必须相同,反过来,一个words[i]对应的也只有一个pattern[i]。

  乱搞:

 class Solution {
public:
bool wordPattern(string pattern, string str) {
set<string> sett[];
map<string,char> mapp;
string t;char c;int pos=;
for(int i=; i<str.size(); i++,pos++)
{
if(pattern.size()==pos) return false;
t="";
c=pattern[pos];
while(i<str.size()&&str[i]==' ') i++;
while(i<str.size()&&str[i]!=' ') t+=str[i++];
sett[c-'a'].insert(t);
if(sett[c-'a'].size()>) return false;
if(!mapp[t]) mapp[t]=c;
else if(mapp[t]!=c) return false;
}
if(pos!=pattern.size()) return false;
return true;
}
};

AC代码

  用流:

 class Solution {
public:
bool wordPattern(string pattern, string str) {
stringstream ss(str);
set<string> sett[];
map<string,char> mapp;
string t;char c;int pos=;
while(getline(ss,t,' '))
{
if(pattern.size()==pos) return false;
c=pattern[pos++];
sett[c-'a'].insert(t);
if(sett[c-'a'].size()>) return false;
if(!mapp[t]) mapp[t]=c;
if(mapp[t]!=c) return false;
}
return pos==pattern.size();
}
};

AC代码

LeetCode Word Pattern (模拟)的更多相关文章

  1. [LeetCode] Word Pattern II 词语模式之二

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  2. [LeetCode] Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...

  3. [LeetCode] Word Pattern

    Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...

  4. Leetcode: Word Pattern II

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  5. Leetcode solution 291: Word Pattern II

    Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...

  6. leetcode面试准备: Word Pattern

    leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...

  7. LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

    翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...

  8. [LeetCode] 290. Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  9. [LeetCode] 290. Word Pattern 单词模式

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

随机推荐

  1. BZOJ1932 [Shoi2007]Setstack 集合堆栈机

    妈呀...clj大爷太强啦! 原来还有set_union和set_intersection这种东西... 于是只要把栈顶的每个元素hash一下记录到一个vector里去就好了 /*********** ...

  2. Java中的String与常量池[转帖]

    string是java中的字符串.String类是不可变的,对String类的任何改变,都是返回一个新的String类对象.下面介绍java中的String与常量池. 1. 首先String不属于8种 ...

  3. centos 5.5 安装 lnmp

    centos5.5 安装 lnmp,一定要事先选好版本安装,建议自己下载安装. 1.相关文件目录: nginx: /www/nginx/下面mysql: /usr/share/mysql /usr/b ...

  4. php中日期的加减法运算

    需求:通过对某个日期增加或减去几天,得到另外一个日期1.首先通过strtotime()获得日期的时间戳2.获得N天前得时间戳,通过”当前时间戳 - N天的秒数 = N天前得时间戳“3.对N天前得时间戳 ...

  5. bzoj 2245: [SDOI2011]工作安排

    #include<cstdio> #include<iostream> #include<cstring> #define M 10000 #define inf ...

  6. POJ 2240 && ZOJ 1082 Arbitrage 最短路,c++ stl pass g++ tle 难度:0

    http://poj.org/problem?id=2240 用log化乘法为加法找正圈 c++ 110ms,g++tle #include <string> #include <m ...

  7. POJ 1719 二分图最大匹配(记录路径)

    Shooting Contest Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4097   Accepted: 1499 ...

  8. UVa 11762 - Race to 1

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. UVa 10561 - Treblecross

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  10. php base64_decode 解码方法

    <?php header('Content-Type:text/html;charset=utf-8'); function encode_file_contents($filename) { ...