LeetCode Word Pattern (模拟)
题意:
给出一个模式串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 (模拟)的更多相关文章
- [LeetCode] Word Pattern II 词语模式之二
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...
- [LeetCode] Word Pattern
Word Pattern Total Accepted: 4627 Total Submissions: 17361 Difficulty: Easy Given a pattern and a st ...
- Leetcode: Word Pattern II
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- Leetcode solution 291: Word Pattern II
Problem Statement Given a pattern and a string str, find if str follows the same pattern. Here follo ...
- leetcode面试准备: Word Pattern
leetcode面试准备: Word Pattern 1 题目 Given a pattern and a string str, find if str follows the same patte ...
- LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...
- [LeetCode] 290. Word Pattern 词语模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
- [LeetCode] 290. Word Pattern 单词模式
Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...
随机推荐
- SSH由WAS/Tomcat/Weblogic迁移到JBOSS
又是一个凌晨,又一次搞项目在新的中间件上的可部署性验证... 原来将项目部署到was7上,花了三个晚上到凌晨1点多的时间,总结出了只要将common-logging和wodenxx.jar两个jar包 ...
- ASP.NET-【状态管理】-Cookie小结
Cookie路径 谷歌浏览器 依次点击设置--高级选项--内容设置--cookies--选择“显示cookies和其他网站数据”按钮就可以看到了 C:\Users\Administrator\Loca ...
- Can't connect to local MySQL server through socket 问题解决
Fedora8启动mysql 报错:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/m ...
- 又一个提示框思密达,腾讯UED
demo <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf- ...
- 使用HTTP访问网络------使用HTTPURLConnection
HTTPURLConnection继承了URLConnection,因此也可用于向指定网站发送GET请求.POST请求.它在URLConnection的基础上提供了如下便捷的方法: 1.int ge ...
- Android为ViewPager增加切换动画——使用属性动画.
ViewPager作为Android最常用的的组件之一,相信大家在项目中会频繁的使用到的,例如利用ViewPager制作引导页.轮播图,甚至做整个app的表现层的框架等等. Android3.0以下不 ...
- 用PHP做服务器接口客户端用http协议POST访问安全性一般怎么做
我的问题是,如果不做安全相关处理的话,一些可能改变数据库的操作可能会遭遇垃圾数据提交什么的,毕竟要找到这些信息只要找个http包就可以了 系统无用户登录 新手问题(从来没做过服务端开发),如果可以,给 ...
- [Js]布局转换
为什么要布局转换? 要这样的效果,单写css,只要给每个li浮动就行,不需要绝对定位.但是比如做一些效果(如鼠标移入图片变大),就需要改变位置了.直接给每个li在css上定好位置不方便,也不知道有几个 ...
- POJ 2739 Sum of Consecutive Prime Numbers 难度:0
题目链接:http://poj.org/problem?id=2739 #include <cstdio> #include <cstring> using namespace ...
- 通过使用ScriptManager.RegisterStartupScript,呈现后台多次使用alert方法
在前台HTML中加入alert或者confirm,相信大家已经非常熟悉并且经常使用: <div onclick="alert('hello')">按钮1</div ...