这是一个编程问题 [英] Its a programming question

查看:84
本文介绍了这是一个编程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定两个相等长度的字符串,你必须判断它们两个字符串是否相同。



两个字符串S1和S2被认为是相同的,如果有的话字符串S1的排列等于字符串S2。有关详细信息,请参阅示例说明。



输入:



第一行,包含一个整数'T'表示不。测试用例。

每个测试由一行组成,包含两个空格分隔的字符串S1和S2,长度相等。

输出:



对于每个测试用例,如果字符串S1的任何排列等于字符串S2,则打印是否则打印否。

约束:



1< = T< = 100

1< = | S1 | = | S2 | < = 10 ^ 5

字符串仅由小写字母组成。

注意:仅使用哈希概念。尝试在O(字符串长度)中进行。



样本输入

3

sumit mitsu

ambuj jumba

abhi hibb

样本输出





NO



我尝试过:



< pre lang =c ++> #include < < span class =code-leadattribute> iostream >
#include < string.h >
使用 命名空间标准

int main()
{
int n,j,f = 0 ,c,d;
char str1 [ 100000 ];
char str2 [ 100000 ];
std :: cin>> n;
while (n--)
{
std :: cin.getline(str1, 100000 );
std :: cin.getline(str2, 100000 );
c = strlen(str1);
d = strlen(str2);
for int i = 0 ; i< c; i ++)
{
for (j = 0 ; j< d; j ++)
{
if (str1 [i] == str2 [j])
{
f ++;
}
}
}

如果(f == c)
{
std :: cout<< \ nYES;
}
else
{
std :: cout<< \ nNO;
}
}

返回 0 ;
}





第一种情况下的输出为YES,但在其他情况下是错误的。

解决方案

您主要编写了比较部分,但是您没有在每一行上将字符串彼此分开。你必须先做到这一点。记住它们被一个空间隔开,所以你要做的就是找到空间。直到空格的字符转到第一个字符串,空格后的字符转到第二个字符串。请记住使用空字符终止字符串或首先将字符串初始化为所有空值。这是一种初始化它们的简单方法:

  char  str1 [ 100000 ] = { 0 }; 



回到你的比较,那不是将成为O(长度)操作。您只需要一个循环来进行比较,您应该将str1 [i]与str2 [i]进行比较。另外,不要先调用strlen,因为这是不必要的。只需循环直到你在字符串中找到一个空字符。你也必须比较那些,因为如果两个字符串在同一个地方没有结尾null,那么它们就不匹配了。


这个问题显然是编程比赛,很高兴提供参考。



- 第一个问题:你使用 f 来计算匹配数,但是在第一次测试后,你忘记了重置下一次测试的价值。



- 第二个问题:你的算法错了。
你的算法


abhi hibb是YES,因为匹配是0 + 2 + 1 + 1 = 4,b匹配2次

hibb abhi是YES因为匹配是1 + 1 + 1 + 1 = 4,2 b匹配相同的b

任何abcdefghijklmnopqrstuvwxyz都是YES,任何字母都找到匹配



[更新]

对于那些感兴趣的人,这里有一些测试用例。如果你正确处理它们,你的算法应该是正确的:

abbccc ccacbb是

aabbcc bcccca否

bcccca aabbcc否

aabbcc abcdef否


#include< iostream>

#include< string.h>

#include< ; stdlib.h>



使用命名空间std;



int main(){

int t,i,j;

cin>> t;

while(t--)

{

char s1 [100000],s2 [100000];

cin>> s1>> s2;

if(strlen(s1)! = strlen(s2))

{

cout<<NO<< endl;

break;
(b = b; i< strlen(s1); i ++)

$
; s1 [i];

for(j = 0; j< strlen(s2); j ++)

{

if(s1 [ i] == s2 [j])

{

if(i ==(strlen(s1)-1))

{

cout<<YES<< ;结束;

}

休息;

}

if(j ==(strlen(s2) - 1)&& s1 [i]!= s2 [j])

{

cout<<NO<< endl;

break ;

}

}

if(s1 [i] == s2 [j])

{

继续;

}



if(j ==(strlen(s2)-1)&& ; s1 [i]!= s2 [j])

{



休息;

}

}

}

}


Given two strings of equal length, you have to tell whether they both strings are identical.

Two strings S1 and S2 are said to be identical, if any of the permutation of string S1 is equal to the string S2. See Sample explanation for more details.

Input :

First line, contains an intger 'T' denoting no. of test cases.
Each test consists of a single line, containing two space separated strings S1 and S2 of equal length.
Output:

For each test case, if any of the permutation of string S1 is equal to the string S2 print YES else print NO.
Constraints:

1<= T <=100
1<= |S1| = |S2| <= 10^5
String is made up of lower case letters only.
Note : Use Hashing Concept Only . Try to do it in O(string length) .

SAMPLE INPUT
3
sumit mitsu
ambuj jumba
abhi hibb
SAMPLE OUTPUT
YES
YES
NO

What I have tried:

#include<iostream>
#include<string.h>
using namespace std;

int main()
{   
    int n,j,f=0,c,d;
    char str1[100000];
    char str2[100000];
    std::cin>>n;
    while(n--)
    {   
        std::cin.getline(str1,100000);
        std::cin.getline(str2,100000);
        c=strlen(str1);
        d=strlen(str2);
        for(int i=0;i<c;i++)
        {
            for(j=0;j<d;j++)
            {
                if(str1[i]==str2[j])
                {
                    f++;
                }
            }
        }
       
        if(f==c)
        {
            std::cout<<"\nYES";
        }
        else 
        {
            std::cout<<"\nNO";
        }
    }
    
    return 0;
}



Its output in 1st case is YES but in other case is wrong.

解决方案

You have the comparison part written, mostly, but you are not separating the strings from each other on each line. You have to do that first. Remember they are separated by a space so all you have to do is find the space. Characters up to the space go to the first string and characters after the space go to the second string. Remember to terminate the strings with a null character or initialize the strings to all nulls first. Here is a simple way to initialize them :

char str1[100000] = {0};


Back to your comparison, that is not going to be an O(length) operation. You only need one loop to do the comparison and you should compare str1[i] with str2[i]. Also, don't call strlen first because that is unnecessary. Just loop until you hit a null character in the string. You also have to compare those too because if both strings do not have their ending null at the same place then they do not match.


This problem is obviously a programming contest, it is nice to give reference.

- first problem: you use f to count matches, but after first test, you forgot to reset its value for next test.

- Second problem: your algorithm is wrong.
with your algorithm
abhi hibb is YES because matches are 0+2+1+1=4, b matched 2 times
hibb abhi is YES because matches are 1+1+1+1=4, the 2 b match the same b
anything abcdefghijklmnopqrstuvwxyz is YES, any letter find a match

[Update]
For those how are interested, here is a few test cases. If you handle them correctly, your algorithm should be correct:
abbccc ccacbb Yes
aabbcc bcccca No
bcccca aabbcc No
aabbcc abcdef No


#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main() {
int t,i,j;
cin>>t;
while(t--)
{
char s1[100000], s2[100000];
cin>>s1>>s2;
if(strlen(s1)!=strlen(s2))
{
cout<<"NO"<<endl;
break;
}
for(i=0;i<strlen(s1);i++)
{
//cout<<s1[i];
for(j=0;j<strlen(s2);j++)
{
if(s1[i]==s2[j])
{
if(i==(strlen(s1)-1))
{
cout<<"YES"<<endl;
}
break;
}
if(j==(strlen(s2)-1) && s1[i]!=s2[j])
{
cout<<"NO"<<endl;
break;
}
}
if(s1[i]==s2[j])
{
continue;
}

if(j==(strlen(s2)-1) && s1[i]!=s2[j])
{

break;
}
}
}
}


这篇关于这是一个编程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆