Similar Word 变序词代码

本文介绍了一个用于判断两个单词是否为相似单词的算法。通过构造一个循环字符串并使用strstr函数进行匹配,可以快速判断两个单词是否可以通过多次循环移位变为彼此。

Similar Word

description

  It was a crummy day for Lur. He failed to pass to the CET-6 (College English Test Band-6). Looking back on how it was in last year gone by, he gradually noticed he had fled too many English Lessons. But he determines to memorize words on his bed ,not in the classroom. You know, it is not that easy to pass the test mainly because the large amount of born words.

     Lur is intelligent on games , never English. He cann't learn the similar words by heart. He

always choose to select a word to learn from the similar words . For him, two words are similar if and only if one word can equal to the other by multiple cyclic shift(at least 1). For example, "car" and "arc" are similar words, while "car" and "rca" are also similar words . To save more time to play games,

  Lur want to know wether two words are similar words faster, he asks you to write a program to tell him ,can you help him ?

                                                    

input

  There are multiple test cases. Each case contains two lines. Each line contains a word,

W. You can assume that length(W)<=10^5 . Ended by EOF.

                                                    

output

  Output “yes” in a single line if two words are similar,otherwise you should output  “no” in a single line.

                                                    

sample_input

car

arc

car

cra

car

car

                                                    

sample_output

yes

no

no

 

 

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
 
//Copyright      感谢Coral提供法思路
 
int main()
{        chars1[100001], s2[100001], temp[200002];
         int  len,len2, i;
 
         while( cin >> s1 >> s2 )
         {        if ( strcmp ( s1, s2 ) != 0 ) //长的一样可不行
                   {        len = strlen ( s1 ); len2= strlen(s2);
                            {        strcpy ( temp, s1 ); //复制组成循环单词
 
                                     for( i = len; i < 2 * len - 1; i++ )
                                     {        temp[i] = s1[i - len];
                                     }
 
                                     //cout<< temp << endl;
                                     if( strstr ( temp, s2 )&& len==len2 ) //查找是否存在该单词序列strstr不能保证单词构成相同,比如car和ca;
                                               cout<< "yes" << endl;
 
                                     else
                                               cout<< "no" << endl;
 
                            }
                   }
 
                   else
                            cout<< "no" << endl;
 
         }
 
 
}



 

转载于:https://www.cnblogs.com/slankka/archive/2013/06/15/9158650.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值