Answer the question
In order to leave comments, you need to log in
How to solve this problem (Olympic)?
Good afternoon, I solve an olympid problem, I get the solution. but the time it takes to get the result is too long. Can you please tell me what other ways to solve this problem? so far I've tried with regex and 3 nested loops (deleted the task with regex) version php 5.6.
I'm not asking you to throw off solutions, I'm asking you to tell me how to solve it with what?
input: 4 width=5 ht=3 len=10 name=circ rad=5 name=circ rad=5 name=sqr width=5 ht=3 4 ht=3 width=5 name=circ name=sqr width=5 len=10
<?
$start = microtime(true);
$handle = @fopen("input.txt", "r");
$products = array();
$properties = array ();
if ($handle) {
$i=0;
while (($buffer = fgets($handle, 4096)) !== false) {
if ($i==0){
$count_products = trim($buffer);
}
if ($i>0 and $i<$count_products+1){
$arrayString = explode(' ',trim($buffer));
$arr = array();
foreach ($arrayString as $string){
$keyValArr= explode('=', $string);
$arr[$keyValArr[0]]=$keyValArr[1];
}
array_push($products, $arr);
}
if ($i==$count_products+1){
$count_properties = trim($buffer);
}
if ($i>$count_products+1){
//array_push($properties, explode(" ", trim($buffer)));
$arrayString = explode(' ',trim($buffer));
$arr = array();
foreach ($arrayString as $string){
$keyValArr= explode('=', $string);
$arr[$keyValArr[0]]=$keyValArr[1];
}
array_push($properties, $arr);
}
$i=$i+1;
}
if (!feof($handle)) {
echo "Ошибка: fgets() неожиданно потерпел неудачу\r\n";
}
fclose($handle);
}
//echo 'Время выполнения скрипта: '.(microtime(true) - $start).' сек.';
$response="";
foreach ($properties as $propertyString=>$arrayInProperty){
$numberProperty = count ($arrayInProperty);
// echo json_encode($arrayInProperty).'<br \>';
$numberProduct = 0;
foreach ($products as $productString=>$arrayInProduct){
$string="";
$index = 0;
foreach ($arrayInProperty as $property=>$value){
if(array_key_exists($property, $arrayInProduct)){
if (trim($arrayInProduct[$property])==$value){
$string = $string.$property.' = '. $arrayInProduct[$property]."<br \ >";
$index = $index+1;
}
else {
break;
}
}else {
// echo '/ not'.'<br \>';
break;
}
}
if ($numberProperty == $index){
$numberProduct = $numberProduct+1;
}
// echo $string;
// echo '<br \>';
}
$response = $response.$numberProduct.PHP_EOL;
}
$fp = fopen("output.txt", "w+");
fwrite($fp,$response);
//echo json_encode($arrayInProperty).'<br \>';
//echo json_encode($properties);
echo 'Время выполнения скрипта: '.(microtime(true) - $start).' сек.';
?>
Answer the question
In order to leave comments, you need to log in
On point 1, I didn’t understand much, but how will this help?
2 point I did so (as evidenced by break. )
if(array_key_exists($property, $arrayInProduct)){ //Если существует свойство у продукта
if (trim($arrayInProduct[$property])==$value){ // если данное свойство равно свойству продукта.
$string = $string.$property.' = '. $arrayInProduct[$property]."<br \ >";
$index = $index+1;
}
else {
break;
}
}else {
// echo '/ not'.'<br \>';
break;
}
Or I did not understand the task, or drive the input data into an associative array, and simply enumerate to form the resulting array for output.
I would abstract into several functions, for simplicity:
- loading data
- bypassing queries
- bypassing product properties
- checking position
- adding result (if there is no branch - adds, if there is - increments)
- displaying results
Number of iterations - product of queries and the number of products.
Ways to reduce the number of iterations, at a glance:
- make an index of product properties
- (regardless of the index) when examining properties, skip (continue) positions with the missing desired property
This is where the ideas end.
PS In the formulation of the problem there is not a word about optimizations, including performance optimization. Perhaps, in this case, the principle of the harm of premature optimization works (the same one that says that premature optimization is harmful) - if you just need to make it just work, you just need to make it just work. No more and no less.
The execution time depends not only on your when, but also on the version of puff to the piece of iron on which you run the process.
The meaning is simple. In input.txt, we first have products, and only after them is a request. We can immediately process each request as soon as we receive it.
The problematic place is array_diff, it can be replaced with isset, but in this case one more cycle will have to be added.
In fact, no role plays.
You can replace it with str_replace and feed it 2 arrays, but in this case you need to count the number of elements
. You can also add caching for searching in the $products array, there will be a small increase in repeated requests.
In addition to this method in the forehead, you can get confused more. For example, work with the stream and "parse" on the go. We immediately become aware of the shift in products and inquiries.
But in this example, we shove all the inputs into memory at once.
<?php
// for ($loop = 0; $loop < 10000; ++$loop) { // for benchmark
$products = [];
$section = $index = 0;
$output = fopen(__DIR__.'/output.txt', 'w+');
foreach (file(__DIR__.'/input.txt', FILE_IGNORE_NEW_LINES) as $line) {
if (false === strpos($line, '=')) {
++$section;
continue;
}
if (1 === $section) {
$products[$index] = explode(' ', $line);
++$index;
continue;
}
$request = explode(' ', $line);
$total = 0;
foreach ($products as $product) {
$total += (int) !array_diff($request, $product);
}
fwrite($output, $total.PHP_EOL);
}
fclose($output);
// }
// for i in {0..10} ; do time php original.php; done
//
// php original.php 0,71s user 0,12s system 99% cpu 0,835 total
// php original.php 0,69s user 0,13s system 99% cpu 0,820 total
// php original.php 0,66s user 0,16s system 99% cpu 0,829 total
// php original.php 0,66s user 0,19s system 99% cpu 0,854 total
// php original.php 0,68s user 0,15s system 99% cpu 0,830 total
// php original.php 0,66s user 0,16s system 99% cpu 0,831 total
// php original.php 0,69s user 0,14s system 99% cpu 0,835 total
// php original.php 0,68s user 0,15s system 99% cpu 0,832 total
// php original.php 0,72s user 0,13s system 99% cpu 0,848 total
// php original.php 0,71s user 0,14s system 99% cpu 0,852 total
// php original.php 0,66s user 0,17s system 99% cpu 0,836 total
// for i in {0..10} ; do time php optimized.php; done
//
// php optimized.php 0,33s user 0,52s system 71% cpu 1,205 total
// php optimized.php 0,38s user 0,43s system 72% cpu 1,112 total
// php optimized.php 0,35s user 0,38s system 70% cpu 1,050 total
// php optimized.php 0,38s user 0,40s system 72% cpu 1,080 total
// php optimized.php 0,35s user 0,41s system 71% cpu 1,055 total
// php optimized.php 0,38s user 0,36s system 74% cpu 0,997 total
// php optimized.php 0,40s user 0,36s system 71% cpu 1,057 total
// php optimized.php 0,40s user 0,37s system 70% cpu 1,087 total
// php optimized.php 0,32s user 0,42s system 74% cpu 0,994 total
// php optimized.php 0,38s user 0,36s system 72% cpu 1,014 total
// php optimized.php 0,36s user 0,36s system 71% cpu 1,025 total
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question