设为首页 加入收藏

TOP

UVA 10941 - Words adjustment(BFS+字符串处理)
2015-07-20 17:48:32 来源: 作者: 【 】 浏览:1
Tags:UVA 10941 Words adjustment BFS 字符串 处理

UVA 10941 - Words adjustment

题目链接

题意:给定两个字符串,在给定一些单词集合,问能否两个单词后面各添加一些单词,使得两个单词变成相同,问添加单词最少几次,单词要来自单词集合

思路:广搜,记录状态为两个字符串之间差的字符,利用set和string去乱搞。。即可

代码:

?

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
      
        #include 
       
         #include 
        
          using namespace std; int t; set
         
           s, vis; string x, y; void init() { cin >> x >> y; string tmp; int q; cin >> q; s.clear(); while (q--) { cin >> tmp; s.insert(tmp); } } struct State { string str; int val; State() {} State (string str, int val) { this->str = str; this->val = val; } }; int solve() { queue
          
            Q; if (x.length() > y.length()) Q.push(State(x.substr(y.length()), 0)); else Q.push(State(y.substr(x.length()), 0)); vis.clear(); vis.insert(Q.front().str); while (!Q.empty()) { State u = Q.front(); Q.pop(); if (u.str == "") return u.val; set
           
            ::iterator now = s.lower_bound(u.str); for (set
            
             ::iterator it = now; it != s.end() && it->substr(0, u.str.length()) == u.str; it++) { string tmp = it->substr(u.str.length()); if (vis.find(tmp) == vis.end()) { vis.insert(tmp); Q.push(State(tmp, u.val + 1)); } } for (int i = u.str.length(); i > 0; i--) { set
             
              ::iterator it = s.find(u.str.substr(0, i)); if (it != s.end()) { string tmp = u.str.substr(it->length()); if (vis.find(tmp) == vis.end()) { vis.insert(tmp); Q.push(State(tmp, u.val + 1)); } } } } return -1; } int main() { scanf("%d", &t); while (t--) { init(); cout << solve() << endl; } return 0; }
             
            
           
          
         
        
       
      
     
    
   
  


?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇uva 11019 - Matrix Matcher(AC自.. 下一篇Leetcode 高精度 Plus One

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C语言结构体怎么直接 (2025-12-24 17:19:44)
·为什么指针作为c语言 (2025-12-24 17:19:41)
·如何较为深入的理解c (2025-12-24 17:19:38)
·Announcing October (2025-12-24 15:18:16)
·MySQL有什么推荐的学 (2025-12-24 15:18:13)